UIViewController.navigationController 变为 nil

发布于 2024-12-26 19:11:13 字数 1778 浏览 5 评论 0原文

我遇到了一个问题,其中 UIViewController.navigationController 变为 nil,我正在拼命寻找这个问题的答案。

UINavigationController 在应用程序委托中进行设置:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootView" bundle:nil];

    UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:self.rootViewController];
    [self.window addSubview:navigationController.view];

    [self.window makeKeyAndVisible];

    return YES;
}

RootViewController 出现时,self.navigationController 成员被设置,我可以用它来隐藏导航栏,如下所示:

- (void)viewWillAppear:(BOOL)animated {
    NSLog( @"self = %@, self.navigationController = %@", self, self.navigationController );
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

调试输出显示 selfself.navigationController 的值。

当在此控制器中单击按钮时,self 确实保持相同的值,但 self.navigationController 现在是 nil

- (IBAction)buttonClicked:(id)sender {
        NSLog( @"self = %@, self.navigationController = %@", self, self.navigationController );
        // here, self.navigationController is nil, so
        // [self.navigationController pushViewController:...] doesn't work :-(
}

我见过几十个有关此问题的问题,答案始终是 UIViewController 不是 UINavigationController 的一部分。由于在 viewWillAppear 中访问 navigationController 工作正常,我相信一定发生了其他事情。你有什么指点吗?如有必要,我很乐意提供更多详细信息。

I bumped into a problem where UIViewController.navigationController becomes nil and I'm desperately trying to find an answer to this one.

The UINavigationController gets setup in the application delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootView" bundle:nil];

    UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:self.rootViewController];
    [self.window addSubview:navigationController.view];

    [self.window makeKeyAndVisible];

    return YES;
}

When the RootViewController is appearing, the self.navigationController member is set and I can use it to hide the navigation bar, like so:

- (void)viewWillAppear:(BOOL)animated {
    NSLog( @"self = %@, self.navigationController = %@", self, self.navigationController );
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

The debug output shows values for self and self.navigationController.

When a button is clicked in this controller, self remains the same value indeed but self.navigationController is now nil:

- (IBAction)buttonClicked:(id)sender {
        NSLog( @"self = %@, self.navigationController = %@", self, self.navigationController );
        // here, self.navigationController is nil, so
        // [self.navigationController pushViewController:...] doesn't work :-(
}

I've seen dozens of questions regarding this problem and the answer is always that the UIViewController is not part of a UINavigationController. Since accessing the navigationController in viewWillAppear works fine, I believe something else must be going on. Do you have any pointers? I'll happily provide more detail if necessary.

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

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

发布评论

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

评论(4

悸初 2025-01-02 19:11:13

在应用程序委托中尝试一下:

[(UINavigationController *)self.window.rootViewController pushViewController:yourViewController animated:NO];

如果您要调试它,rootviewcontroller 实际上是 UINavigationController 。这对我有用。

Try this in the app delegate:

[(UINavigationController *)self.window.rootViewController pushViewController:yourViewController animated:NO];

the rootviewcontroller is actually UINavigationController if you po to debug it. This works for me.

绝影如岚 2025-01-02 19:11:13

您的代码显示您仅使用 navigationController 的视图,但只是祈祷 navigationController 的生命由某些魔手处理,但事实并非如此。

您需要有人成为此处的 navigationController 的显式所有者。

事实上,以下行:

[self.window addSubview:navigationController.view];

似乎表明您想要的是窗口的 rootViewController 成为 navigationController:

self.window.rootViewController = navigationController;

而且,应用程序的委托似乎也成为 navigationController 的所有者,因此 navigationController 实际上应该是您的应用程序委托的 ivar。

简而言之,修复您的对象图(并且它会巧合地执行您手动执行的额外保留并修复您的错误)

Your code shows that you are only using the navigationController's view but just pray that navigationController life is handled by some magic hand which is not the case.

You need someone to be the explicit owner of the navigationController here.

In fact, the following line:

[self.window addSubview:navigationController.view];

seems to indicate that what you want is for the window's rootViewController to be navigationController:

self.window.rootViewController = navigationController;

But also, it seems that the application's delegate is to be an owner of navigationController as well so navigationController should, in fact, be an ivar of your app's delegate.

In short, fix your object graph (and it will coincidentally do the extra retain you manually did and fix your bug)

清泪尽 2025-01-02 19:11:13

我遇到了 nil 视图控制器的问题,发现它在故事板中没有正确连接到应用程序委托。

I had a problem with a nil view controller and found that it was not connected properly in storyboard to the app delegate.

深巷少女 2025-01-02 19:11:13

与往常一样,提出问题只是为了在几分钟后找到解决方案,这会有所帮助。

我想我是 ARC 的牺牲品了。当我在应用程序委托中保留 UINavigationController 后,它就工作得很好。

As always, it helps to formulate the question just to find the solution some minutes later.

I fell prey to ARC, I guess. Once I retained the UINavigationController in the application delegate, it worked fine.

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