iPhone - 从 xib 加载视图控制器时忽略 UINavigationItem

发布于 2024-10-21 05:31:53 字数 659 浏览 2 评论 0原文

我在 UINavigationController 中有一个 UIViewController,并且我将第二个 UIViewController 从 XIB 文件推送到导航堆栈上。此 XIB 还包含一个用于第二个视图控制器的标题和按钮的 UINavigationItem,但在加载 XIB 时这些会被忽略。有什么想法吗?

这是我的代码(photoViewController 是第二个 viewController)

- (void) displayPhotoWithId:(int)photoId {

    if (_photoViewController == nil) {
        self.photoViewController = [[[PhotoViewController alloc] initWithNibName:@"PhotoView" bundle:[NSBundle mainBundle]] autorelease];        
    }

    _photoViewController.photoId = photoId;

    [self.navigationController pushViewController:_photoViewController animated:YES]; 

}

I have a UIViewController within a UINavigationController, and I'm pushing a second UIViewController onto the navigation stack from a XIB file. This XIB also includes a UINavigationItem for the second view controller's title and button, but these are ignored when loading the XIB. Any ideas?

Here is my code (photoViewController is the second viewController)

- (void) displayPhotoWithId:(int)photoId {

    if (_photoViewController == nil) {
        self.photoViewController = [[[PhotoViewController alloc] initWithNibName:@"PhotoView" bundle:[NSBundle mainBundle]] autorelease];        
    }

    _photoViewController.photoId = photoId;

    [self.navigationController pushViewController:_photoViewController animated:YES]; 

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心 2024-10-28 05:31:53

您可以从界面生成器中将其连接起来。将类似的内容添加到您的视图控制器中:

IBOutlet UINavigationItem* navigationItem;

然后将其连接到视图控制器出口(大概从文件所有者连接到 XIB 中的 UINavigationItem )。我已经这样做了并且效果很好。我想 UINavigationController 会自动查找“navigationItem”。

You can wire this up from interface builder. Add something like this to your view controller:

IBOutlet UINavigationItem* navigationItem;

Then wire it up to the view controller outlets (from File Owner, presumably, to the UINavigationItem in your XIB). I have done this and it works fine. I suppose UINavigationController looks for the 'navigationItem' automagically.

执手闯天涯 2024-10-28 05:31:53

导航项被忽略,因为它与该控制器无关,它只是碰巧位于该控制器的 XIB 中。控制器的导航项是在实例化时创建的。您必须在代码中设置标题和导航按钮(例如在 viewDidLoad 中),因为控制器没有 navigationItem 出口。 XIB 中的 UINavigationItem 仅在 UINavigationController 的根控制器内工作。

Navigation item is ignored because it has nothing to do with this controller, it's just happened to be in this controller's XIB. Controller's navigation item is created on instantiation. You have to setup title and navigation buttons in code (in viewDidLoad for example) because controller doesn't have navigationItem outlet. UINavigationItem in XIB works only inside UINavigationController's root controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文