UINavigation 控制器数组错误

发布于 2024-11-07 06:23:12 字数 1272 浏览 0 评论 0原文

我只是想知道弹出 UINavigationController 时遇到的一个错误。无论我当前使用哪个 UIViewController,我都实现了一个注销控制器,该控制器会弹出到 UINavigationController 中的根控制器。此处列出了执行此操作的主要代码。

NSLog(@"Root Controller: %@", [[self.navigationController.viewController objectAtIndex:0] class]);
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
NSLog(@"Root Controller: %@", [[self.navigationController.viewController objectAtIndex:0] class]);

这是控制台显示的内容。

Root Controller: DetailViewController
Root Controller: (null)

但是,当我将上面的代码更改为:

NSLog(@"Root Controller: %@", [[self.navigationController.viewController objectAtIndex:0] class]);
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
NSLog(@"Root Controller: %@", [[self.navigationController.viewController objectAtIndex:0] class]);

控制台中的结果显示为:

Root Controller: DetailViewController
Root Controller: DetailViewController

为什么当我将 UINavigationController 弹出到 DetailViewController 时,它没有触发 ViewWillAppear 方法来更新我的登录信息,这让我困扰了一段时间。在我看来,当我弹出到 0 索引时,它正在访问一个越界的数组对象,但是当我弹出到索引 1 时,这就是当前根控制器所在的位置。有谁知道这会是什么原因?

I was just wondering about a bug I came across when popping my UINavigationController. Whatever UIViewController I am currently in, I have implemented a logout controller that pops back to the root controller in my UINavigationController. The main code that does this is listed here.

NSLog(@"Root Controller: %@", [[self.navigationController.viewController objectAtIndex:0] class]);
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
NSLog(@"Root Controller: %@", [[self.navigationController.viewController objectAtIndex:0] class]);

This is what the console displays.

Root Controller: DetailViewController
Root Controller: (null)

However when I change the above code to:

NSLog(@"Root Controller: %@", [[self.navigationController.viewController objectAtIndex:0] class]);
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
NSLog(@"Root Controller: %@", [[self.navigationController.viewController objectAtIndex:0] class]);

The results in the console is displayed as:

Root Controller: DetailViewController
Root Controller: DetailViewController

It was bothering me for a while why when I popped the UINavigationController to the DetailViewController that it didnt fire the ViewWillAppear method to update my login information. It seems to me that when I pop to the 0 index it is accessing an array object that is out of bounds but when I pop to index 1, thats where the current root controller is. Does anyone know what the cause of this would be?

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

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

发布评论

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

评论(1

℡寂寞咖啡 2024-11-14 06:23:12

我的猜测是,在将 self 从堆栈中弹出后,self.navigationController 被设置为 nil。

如果是这种情况,看起来弹出到索引 1 处的视图控制器不会将 self.navigationController 重置为 nil (因为 self 是索引 1 处的视图控制器,或者viewWill|Did?Appear 中的一些调用正在再次设置它)。

My guess is that self.navigationController is being set to nil after self is popped off the stack.

If that's the case, it looks like popping to the view controller at index 1 does not reset self.navigationController to nil (either because self is the view controller at index 1 or some call in viewWill|Did?Appear is setting it again).

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