iOS:如何获取指向导航控制器的指针

发布于 2024-12-09 14:33:36 字数 503 浏览 1 评论 0原文

我显然错过了一些东西......

在我的 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 技术交流群。

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

发布评论

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

评论(2

你是我的挚爱i 2024-12-16 14:33:36

navigationController 将无法在 viewDidLoad 上正确设置。您必须在 viewDidAppear 或稍后的某个阶段检查它。您在哪个方法中调用 [self navigationController]

原因是当调用 viewDidLoad 时,UINavigationController 仍在处理pushViewController:animated: 方法。它似乎在初始化控制器的view之后设置了navigationController属性。我不记得该属性是否在 viewWillAppear 运行时设置,但它肯定应该由 viewDidAppear 设置。

The navigationController won't be set properly on viewDidLoad. You have to check it in viewDidAppear or at some later stage. Which method are you calling to [self navigationController] in?

The reason for this is that when viewDidLoad is called, the UINavigationController is still processing the pushViewController:animated: method. It would appear to set the navigationController property after it initialises the controller's view. I can't recall whether the property is set by the time viewWillAppear runs, but it should definitely be set by viewDidAppear.

痕至 2024-12-16 14:33:36
id rootViewController = [[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] nextResponder];
id rootViewController = [[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] nextResponder];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文