Dealloc 未调用更改选项卡 [英] Dealloc not called changing tab

查看:28
本文介绍了Dealloc 未调用更改选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个标签栏控制器,有两个标签,A 和 B,其中 A 是导航控制器.

Let's suppose to have a tab bar controller with two tabs, A and B, where A is a navigation controller.

当用户在A时,他可以推A1,然后是A2,这两个都是视图控制器.A2 上的后退按钮,执行:

When the user is in A, he can push A1 and then A2, which are both view controllers. A back button on A2, performs:

[self.navigationController popViewControllerAnimated:YES];

正确触发 A2 上的 dealloc 方法.

which correctly triggers the dealloc method on A2.

如果用户在A2然后切换到tab B,我需要在A2上调用dealloc方法;因此我在 TabBarController 中实现了以下方法:

If the user is in A2 and then switches to tab B, I need the dealloc method to be called on A2; therefore I've implemented the following method in the TabBarController:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    UINavigationController *nc = (UINavigationController*)tabBarController.selectedViewController;
    [nc popToRootViewControllerAnimated:YES];

    return YES;
}

但是在这个流程中,A2 的 dealloc 方法永远不会被调用!怎么可能从 A2 弹出到 A1 它可以工作,而更改选项卡视图控制器没有被释放?

but in this flow the dealloc method of A2 is never called! How is it possible that popping from A2 to A1 it works, whereas changing tabs the view controller is not deallocated?

感谢您的提示!

推荐答案

didSelectViewController 似乎是在标签控制器切换到目标标签之前触发的.因此,方法 popToRootViewControllerAnimated 的执行正确弹出所有视图控制器,但不能真正释放可见的,因为它当前正在被应用程序描述.

It seems that didSelectViewController is triggered before the tab controller has switched to the destination tab. Therefore the execution of the method popToRootViewControllerAnimated correctly pops all the view controllers, but can't really release the visible one, as it is currently being depicted by the app.

为了达到我的结果,我想出了以下解决方案.在每个选项卡的第一个视图控制器对应的所有视图控制器的viewDidAppear方法中,我执行popToRootViewControllerAnimated(它们都是导航控制器).

To achieve my result, I've figured out the following solution. In the viewDidAppear method of all the view controllers which correspond to the first view controller for each tab, I execute popToRootViewControllerAnimated (all of them are navigation controller).

这篇关于Dealloc 未调用更改选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆