I’m obviously missing something…
In my iOS app, I push a UIViewController onto a navigation controller:
MyViewController *mvc = [[MyViewController alloc] initWithNibName:@"MyViewController"];
[self.navigationController mvc animated:YES];
MyViewController displays fine, and I can see and use the navigationBar, but when I try to get a pointer back to the navigation controller from within my view controller, I get a nil result.
UINavigationController *nav = [self navigationController];
if (!nav) {
NSLog(@"no nav");
}
I’ve been beating my head against this all day, but can’t see that I’m doing anything wrong. I get no warnings or errors in Xcode. Am I completely missing something?
TIA: john
The
navigationControllerwon’t be set properly onviewDidLoad. You have to check it inviewDidAppearor at some later stage. Which method are you calling to[self navigationController]in?The reason for this is that when
viewDidLoadis called, theUINavigationControlleris still processing thepushViewController:animated:method. It would appear to set thenavigationControllerproperty after it initialises the controller’sview. I can’t recall whether the property is set by the timeviewWillAppearruns, but it should definitely be set byviewDidAppear.