2010-09-18 48 views
1

我有这样的代码:为什么self.navigationController成为零?

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

我的第一行,self.navigationController具有值。

在第二行,它的值变为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]; 
+0

它没有区别。有一点需要注意的是,我正在使用基于视图的应用程序,而不是基于导航的应用程序。 – 2010-09-18 03:29:55

+2

如果视图控制器的代码是其中一部分的视图控制器不在UINavigationController的堆栈上,那么'navigationController'属性始终为零(除了具有非常严重的内存垃圾错误的程序之外)。 – imaginaryboy 2010-09-18 03:41:37