ebeth
ebeth

Reputation: 163

Navigation Controller is null

I have a split-view app that allows a user to select and display a thumbnail of a chosen image. I have placed a UIButton in the detailViewController using Interface Builder. When this button is pressed, I would like to have it change to a full screen view of the image. I have set up a new View Controller, called FullViewController and thought I had everything connected. The problem is that the navigation controller is null. I adjusted the AppDelegate.m to the following:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
// Override point for customization after app launch.
// Set the split view controller as the window's root view controller and display.
self.window.rootViewController = self.splitViewController;
UINavigationController *nvcontrol =[[UINavigationController alloc] initWithRootViewController:fullViewController];
[window addSubview:nvcontrol.view];
[self.window makeKeyAndVisible];
return YES;
}

This is the function in the DetailViewController.m which is called when the button is pressed. The navigation controller comes up null in here.

//Function called when button is pressed - should bring up full screen view
- (IBAction) pressFullViewButtonFunction: (id) sender{
//viewLabel.text = @"Full View";    
if (fullViewController == nil){
    FullViewController *fullViewController = [[FullViewController alloc] initWithNibName:@"FullViewController" bundle:[NSBundle mainBundle]];
    NSLog(@"fullViewController is %@", fullViewController);
    self.fullViewController =  fullViewController;
}
NSLog(@"self.navigationController is %@",self.navigationController);//this is null
[self.navigationController pushViewController:self.fullViewController animated:YES];

}

I'm not sure how to fix this. I've tried adding in the couple lines in the AppDelegate, but when it runs, the table in the root view doesn't show up and it no longer properly switches between portrait and landscape views.

I have the rest of the code readily available if that would help clarify. Just let me know! Thanks.

Upvotes: 1

Views: 2601

Answers (1)

sergio
sergio

Reputation: 69047

From the code you post it is not possible to identify the problem, but two common reasons for self.navigationController to be nil are:

  1. you did not push the object behind self on to the navigation controller in the first place; indeed it seems so, since the navigation controller is added as a subview of the split view controller; possibly you mean the opposite... not sure...

  2. (sub-case of 1) you showed the object behind self using presentViewControllerModally.

When I say "the object behind self" I mean the instance of the class where pressFullViewButtonFunction is defined.

If you need more help, post the code where you push your controllers on to the navigation controller...

On a side note, if you do:

UINavigationController *nvcontrol =[[UINavigationController alloc] initWithRootViewController:fullViewController];

and nvcontrol is not an ivar, then you have a leak.

Hope this helps...

Upvotes: 1

Related Questions