iOS - NavigationController 认为同一个控制器被推了两次

发布于 2024-12-01 14:41:07 字数 603 浏览 0 评论 0原文

我对下面的代码有点困惑。如果我注释掉第二条语句,它会成功显示视图:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

delegate.navController.viewControllers = [NSArray arrayWithObject:aViewController];

[delegate.navController pushViewController:aViewController animated:YES];

[aViewController release];

否则,它会在以下情况下崩溃:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported 

我在应用程序委托中添加了一个不同的视图控制器,但不是这个。是什么让它认为这是同一个?

I'm a bit confused about the following code. If I comment out the second statement, it successfully shows the view:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

delegate.navController.viewControllers = [NSArray arrayWithObject:aViewController];

[delegate.navController pushViewController:aViewController animated:YES];

[aViewController release];

Otherwise, it crashes on the following:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported 

I add a different view controller in the app delegate, but not this one. What could be making it think it's the same one?

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

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

发布评论

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

评论(1

南城追梦 2024-12-08 14:41:07
delegate.navController.viewControllers = [NSArray arrayWithObject:aViewController];

[delegate.navController pushViewController:aViewController animated:YES];

第一行将 aViewController 设置为 navController 的唯一控制器。第二行再次将 aViewController 推送到 navController ,所以是的,难怪你会得到它两次。根据您想要做什么,放弃这两行之一。

如果你想将 aViewController 设置为 navController 上的唯一控制器,请保留第一行。

如果你想将 aViewController 作为 navController 上的新控制器推送,请保留第二行。

delegate.navController.viewControllers = [NSArray arrayWithObject:aViewController];

[delegate.navController pushViewController:aViewController animated:YES];

First line set's aViewController as navController's only controller. Second line pushes aViewController to navController again so yeah, no wonder you get it twice. Depending on what you want to do, ditch one of those two lines.

If you want to set aViewController as only controller on navController, keep first line.

If you want to push aViewController as a new controller on navController, keep second line.

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