views:

53

answers:

2

HI I am working on UITablview based project. i like to load a new view when ever a cell got click. i am using following code in didselectRowAtIndexPath delegate method. The below coding is not showing any error but new view not get load and [self navigationController] is returning null.

inside Viewdidload function [self navigationcontroller] returning proper value. but inside Delegate method [self navigationcontroller] returns null.

  /// inside appDelegate class
      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// Override point for customization after application launch.

self.viewController = [[LAMainViewController_iPhone alloc]initWithNibName:@"LAMainViewController_iPhone" bundle:nil];

UINavigationController *controller = [[UINavigationController alloc]initWithRootViewController:viewController];

[window addSubview:controller.view];

[controller release];

[window makeKeyAndVisible];

return YES;
}




  /// inside LAmainviewcontroller.m


    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {

    // Custom initialization
    }

//self.navigationController.navigationBar.backgroundColor =[UIColor clearColor];// .title=@"test";

return self;
     }



       - (void)viewDidLoad {

    [super viewDidLoad];

     NSString *materialPlistPath = [[NSBundle mainBundle]pathForResource:@"materials" ofType:@"plist"];

materialList = [[NSArray alloc]initWithContentsOfFile:materialPlistPath];

materialTable.backgroundColor = [UIColor blackColor];

NSLog(@" dud kiad navigationController  %@", self.navigationController);

//2010-10-20 15:22:03.809 LabAssistant[17368:207]  dud kiad navigationController  <UINavigationController: 0x5f3b160>

   }

    -(void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:YES];

self.navigationController.navigationBarHidden = YES;

NSIndexPath *indexPath = [materialTable indexPathForSelectedRow];

[materialTable deselectRowAtIndexPath:indexPath animated:YES];

    }





  -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

  LAMaterialPropertiesViewController_iPhone *materialPropertyListView = [[[LAMaterialPropertiesViewController_iPhone alloc] initWithNibName:@"LAMaterialPropertiesViewController_iPhone" bundle:nil] autorelease];

  materialPropertyListView.chemicalName = [[materialList objectAtIndex:[indexPath row]] objectForKey:@"materialProperty"];

  [[self navigationController] pushViewController:materialPropertyListView animated:YES];
  NSLog(@"%@",[self navigationController]);

 ///2010-10-20 16:20:42.634 LabAssistant[17656:207]  navigationController  (null)
 }

please help me to fix this issue. thanks!

A: 

Remove the autorelease from the init statement. Actually the viewcontroller is getting released before it gets pushed.

instead of

LAMaterialPropertiesViewController_iPhone *materialPropertyListView = [[[LAMaterialPropertiesViewController_iPhone alloc] initWithNibName:@"LAMaterialPropertiesViewController_iPhone" bundle:nil] autorelease];

try this

LAMaterialPropertiesViewController_iPhone *materialPropertyListView = [[LAMaterialPropertiesViewController_iPhone alloc] initWithNibName:@"LAMaterialPropertiesViewController_iPhone" bundle:nil];

Hope this will solve your problem.

Atulkumar V. Jain
@Atulkumar V. Jain. i have tried but its still its not working [self navigationcontroller] is returning null.
Ram
This may be bcoz the navigationbar is hidden. set the navigationbar.hidden to NO and then let me know.
Atulkumar V. Jain
check with UINavigationController *controller = [[[UINavigationController alloc]initWithRootViewController:viewController] retain];
Gyani
@Atulkumar V. Jain there is no change in output eventhough we set navigationbar.hidden = NO.
Ram
like Gyani said try to retain the instance of navigationcontroller
Atulkumar V. Jain
@Gyani Thanks Gyani, issue fixed :) once i retain the view controller mycoding is working fine. Once Again Thanks.
Ram
A: 

I think the table view delegate is called before the navigation controller is set to the view controller.

Can you try reloading the tableview [tableview reloadData]; in viewWillAppear or viewDidAppear?

xCode