views:

33

answers:

1

I have this code:

ViewController2 *childView = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
[self.navigationController pushViewController:childView animated:YES];

I the first line, self.navigationController has a value.

On the second line, its value becomes 0. Why is that?

+1  A: 

Unless you have overridden initWithNibName:bundle: in ViewController2 to do something truly unfortunate, there's extremely little chance that what you're saying can be true.

If you alter your code with this following assertion, does the assertion really trigger?

UINavigationController* navigationController = self.navigationController;
ViewController2 *childView = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
NSAssert(navigationController == self.navigationController, @"I was removed from the navigation controller!");
[self.navigationController pushViewController:childView animated:YES];
imaginaryboy
It doesn't make a difference. One thing to note is that I am using a view-based application, not a nav-based application.
awakeFromNib
If the view controller that code is a part of is not on the stack of a UINavigationController then the `navigationController` property is nil ... always (with the exception of a program with very serious memory trashing bugs).
imaginaryboy