popToRootViewController 不适用于 UINavigationController 数组
我有一个带有 UINavigationController 数组的 UITabBarController(如 iPod 应用程序),这样我就可以切换选项卡,并且每个选项卡中都有一堆视图控制器。
如果用户触摸新选项卡,我希望能够将所有视图控制器弹出回根目录,因此实现了 UITabBarController 委托方法:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
// Pop all view controllers back to the root view controller
for (UINavigationController *nc in tabBarController.viewControllers){
[nc popToRootViewControllerAnimated:NO];
}
}
为了检查它是否正常工作,我在各个 viewController 的 dealloc 方法上放置了一个断点。
看来我要切换到的选项卡会弹出到根视图控制器,但我要切换的选项卡不会弹出。我在这里遗漏了一些明显的东西?我有 4 个选项卡,for 循环执行了 4 次,并对 popToRootViewController 进行了 4 次单独的调用。
这很重要的原因是,如果我位于 rootViewController 处,那么我可以安全地更新底层数据库,而不会让用户处于不再有意义或有效的视图中。例如,根有一个项目表,触摸某个项目即可获得有关该项目的详细信息。如果用户触摸另一个选项卡,则详细信息项视图控制器仍在内存中。如果应用程序收到此记录不再有效的更新,则按 Tab 返回该记录将导致崩溃或不一致的状态。
希望这是有道理的,任何帮助都会有用,即使它只是关于如何解决问题的想法。
问候
戴夫
这一切都源于这样一个事实:我的应用程序过去常常在启动时检查更新,丢弃旧数据库并使用新数据库。使用 iOS4 及更高版本,我的应用程序不再被杀死,因此还必须检查 applicationWillEnterForeground 中的更新,主要区别是应用程序不处于已知状态。
I have a UITabBarController with an array of UINavigationControllers (like the iPod app) so I can switch tabs and each tab have a stack of view controllers within it.
I want to be able to pop all view controllers back to the root if the users touches a new tab so have implemented the UITabBarController delegate method:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
// Pop all view controllers back to the root view controller
for (UINavigationController *nc in tabBarController.viewControllers){
[nc popToRootViewControllerAnimated:NO];
}
}
To check this is working properly I have put a break point on the dealloc method of my various viewControllers.
It seems the tab I am switching to pops to the root view controller, but the tab I am switching from doesn't. I am missing something obvious here? I have 4 tabs and the for loop gets executed 4 times, and makes 4 separate calls to popToRootViewController.
The reason this is important is if I am at the rootViewController it is safe for me to update the underlying database without leaving the user in a view that no longer makes sense or is valid. For example, the root has a table of items, touching an item gives you detail about that item. If the user touches another tab the detail item view controller is still in memory. If the app receives an update where this record is no longer valid then tabbing back to it will cause a crash or inconsistent state.
Hope that makes sense and any help would be useful even if it is simply ideas on how to troubleshoot the problem.
Regards
Dave
P.S.
This all stems from the fact that my app used to check for an update at startup throw out the old database and use the new one. With iOS4 and above my app doesn't get killed anymore so have to check for the update in applicationWillEnterForeground as well, the major difference being the app is not in a known state.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有一个解决办法,虽然我不确定它为什么有效,但很高兴接受更全面的解释。
我还实现了其他 UITabBarController 方法 shouldSelectViewController
如果动画标志设置为 YES,则此方法有效。
I have a work around, although I am not sure why it works, so happy to accept a more fuller explanation.
I have also implemented the other UITabBarController method shouldSelectViewController
This works PROVIDED the animated flag is set to YES.