【问题标题】:Why does self.navigationController become nil?为什么 self.navigationController 会变成 nil?
【发布时间】:2011-04-13 23:49:21
【问题描述】:

我有这个代码:

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

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

在第二行,它的值变成了 0。这是为什么呢?

【问题讨论】:

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


    【解决方案1】:

    除非您在 ViewController2 中重写了 initWithNibName:bundle: 来做一些真正不幸的事情,否则您所说的内容几乎不可能是真的。

    如果您使用以下断言更改您的代码,该断言真的会触发吗?

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

    【讨论】:

    • 没什么区别。需要注意的一点是,我使用的是基于视图的应用程序,而不是基于导航的应用程序。
    • 如果代码所属的视图控制器不在 UINavigationController 的堆栈中,则 navigationController 属性为 nil ... 总是(除了具有非常严重的内存垃圾的程序错误)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多