iOS:如何获取指向导航控制器的指针
我显然错过了一些东西......
在我的 iOS 应用程序中,我将 UIViewController 推到导航控制器上:
MyViewController *mvc = [[MyViewController alloc] initWithNibName:@"MyViewController"];
[self.navigationController mvc animated:YES];
MyViewController 显示正常,我可以看到并使用导航栏,但是当我尝试从在我的视图控制器中,我得到零结果。
UINavigationController *nav = [self navigationController];
if (!nav) {
NSLog(@"no nav");
}
我一整天都在为此绞尽脑汁,但看不出我做错了什么。我在 Xcode 中没有收到任何警告或错误。我完全错过了什么吗?
蒂亚:约翰
I'm obviously missing something...
In my iOS app, I push a UIViewController onto a navigation controller:
MyViewController *mvc = [[MyViewController alloc] initWithNibName:@"MyViewController"];
[self.navigationController mvc animated:YES];
MyViewController displays fine, and I can see and use the navigationBar, but when I try to get a pointer back to the navigation controller from within my view controller, I get a nil result.
UINavigationController *nav = [self navigationController];
if (!nav) {
NSLog(@"no nav");
}
I've been beating my head against this all day, but can't see that I'm doing anything wrong. I get no warnings or errors in Xcode. Am I completely missing something?
TIA: john
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
navigationController
将无法在viewDidLoad
上正确设置。您必须在viewDidAppear
或稍后的某个阶段检查它。您在哪个方法中调用[self navigationController]
?原因是当调用 viewDidLoad 时,UINavigationController 仍在处理pushViewController:animated: 方法。它似乎在初始化控制器的
view
之后设置了navigationController
属性。我不记得该属性是否在viewWillAppear
运行时设置,但它肯定应该由viewDidAppear
设置。The
navigationController
won't be set properly onviewDidLoad
. You have to check it inviewDidAppear
or at some later stage. Which method are you calling to[self navigationController]
in?The reason for this is that when
viewDidLoad
is called, theUINavigationController
is still processing thepushViewController:animated:
method. It would appear to set thenavigationController
property after it initialises the controller'sview
. I can't recall whether the property is set by the timeviewWillAppear
runs, but it should definitely be set byviewDidAppear
.