【问题标题】:Why won't this code load my NIB/XIB为什么这段代码不能加载我的 NIB/XIB
【发布时间】:2009-09-19 18:17:06
【问题描述】:

我有 2 个带有相应视图控制器的 XIB。将它们称为 MainViewController 和 AboutViewController。 XIB 的名称与 ViewController 的名称完全相同。在 MainViewController 中单击按钮后,我想显示 AboutViewController。

我在 MainViewController 中有这样的代码:

- (IBAction) infoButtonAction:(id)sender {
    AboutViewController *aboutViewController = [[AboutViewController alloc] init];
    [[self navigationController] pushViewController:aboutViewController animated:YES];
}

但是什么也没发生,AboutViewController 没有出现。我也试过这条线:

AboutViewController *aboutViewController = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];

但无济于事。我错过了什么?

【问题讨论】:

    标签: iphone xcode viewcontroller


    【解决方案1】:

    在 Interface Builder 中检查您的 AboutViewController.xib,并确保在类检查器中将 File Owner 设置为 AboutViewController。还要确保将文件所有者的view 属性连接到 xib 中的视图。最后,显示它的代码应该是:

    - (IBAction) infoButtonAction:(id)sender {
        AboutViewController *aboutViewController = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
        [[self navigationController] pushViewController:aboutViewController animated:YES];
        [aboutViewController release];
    }
    

    另外,您应该在同一方法中检查 this 是否为非零:

    UINavigationController * nc = self.navigationController;
    

    【讨论】:

    • 你是对的。 self.navigationController 为零。你能告诉我为什么以及我能做些什么吗?
    • 这意味着您的 MainViewController 不在 navigationController 中。您需要检查如何创建 MainViewController。
    【解决方案2】:

    你有错误吗?您是否检查过按钮在 IB 中的接线是否正确?

    您是否尝试过使用模态视图控制器?

    【讨论】:

    • 我没有收到任何错误。该按钮已正确连接,因为它在 click 函数内的断点处停止。
    猜你喜欢
    • 1970-01-01
    • 2011-04-02
    • 2012-07-06
    • 2022-11-23
    • 1970-01-01
    • 2010-10-24
    • 2021-04-21
    • 2011-01-31
    相关资源
    最近更新 更多