UINavigation 控制器数组错误
我只是想知道弹出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是,在将
self
从堆栈中弹出后,self.navigationController
被设置为 nil。如果是这种情况,看起来弹出到索引 1 处的视图控制器不会将 self.navigationController 重置为 nil (因为
self 是索引 1 处的视图控制器,或者viewWill|Did?Appear 中的一些调用正在再次设置它)。
My guess is that
self.navigationController
is being set to nil afterself
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 becauseself
is the view controller at index 1 or some call in viewWill|Did?Appear is setting it again).