为什么self.navigationController变成了nil?

时间:2010-09-18 01:39:46

标签: objective-c cocoa-touch xcode pointers uiviewcontroller

我有这段代码:

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

我是第一行,self.navigationController有一个值。

在第二行,它的值变为0.为什么?

1 个答案:

答案 0 :(得分:2)

除非你已经覆盖了initWithNibName:bundle:在ViewController2中做一些真正不幸的事情,你所说的话几乎没有机会成真。

如果使用以下断言更改代码,断言是否真的会触发?

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];