iPhone - 从 xib 加载视图控制器时忽略 UINavigationItem
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以从界面生成器中将其连接起来。将类似的内容添加到您的视图控制器中:
然后将其连接到视图控制器出口(大概从文件所有者连接到 XIB 中的 UINavigationItem )。我已经这样做了并且效果很好。我想 UINavigationController 会自动查找“navigationItem”。
You can wire this up from interface builder. Add something like this to your view controller:
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 supposeUINavigationController
looks for the 'navigationItem' automagically.导航项被忽略,因为它与该控制器无关,它只是碰巧位于该控制器的 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 havenavigationItem
outlet. UINavigationItem in XIB works only inside UINavigationController's root controller.