调用popToRootViewControllerAnimated时未调用viewWillDisappear

时间:2022-11-26 18:22:56

I work on a legacy application, and have found out, that my view[Will/Did]Disappear methods are not always fired properly.

我在遗留应用程序上工作,并且发现我的视图[Will / Did]消失方法并不总是被正确解雇。

The case is, I have a (custom) UIViewController set as rootViewController in AppDelegate. This rootViewController has a UINavigationController, which has two view controllers pushed on it. When the user presses the home button, the user is logged out. When he later returns to the app, the application calls [UINavigationController popToRootViewControllerAnimated:YES] and then displays a modal UIViewController for logging in.

情况是,我在AppDelegate中将(自定义)UIViewController设置为rootViewController。这个rootViewController有一个UINavigationController,它上面有两个视图控制器。当用户按下主页按钮时,用户退出。当他稍后返回应用程序时,应用程序调用[UINavigationController popToRootViewControllerAnimated:YES],然后显示用于登录的模式UIViewController。

The problem is: When I push/pop on the UINavigationController normally, my viewWillDisappear method is called properly. But when I use the popToRootViewControllerAnimated: method, viewWillDisappear is not called on any of the viewControllers that are popped off.

问题是:当我正常推送/弹出UINavigationController时,我的viewWillDisappear方法被正确调用。但是当我使用popToRootViewControllerAnimated:方法时,不会弹出任何弹出的viewControllers上的viewWillDisappear。

Searching on the internet has only given two possible reasons:

在互联网上搜索只能给出两个可能的原因:

None of these suggestions are the case in my app. And I have no idea where to look. Anybody has a suggestion to what has been done wrong in the app?

我的应用程序中没有这些建议。我不知道在哪里看。有人建议应用中出错了吗?

2 个解决方案

#1


16  

The view probably wasn't onscreen. It has to be onscreen (visible) for the viewWillDisappear: method to be called. If it's coming back from the background, it wasn't visible.

视图可能不在屏幕上。它必须在屏幕上(可见)才能调用viewWillDisappear:方法。如果它从后台返回,则不可见。

You could try using willMoveToParentViewController: which is called when the view controller is removed from its parent.

您可以尝试使用willMoveToParentViewController:当视图控制器从其父控件中删除时调用。

#2


0  

such useful to me

对我有用

[nav performSelector:@selector(popToRootViewControllerAnimated:) withObject:nil afterDelay:0.0];

I rewrote UITabBarController

我重写了UITabBarController

- (void)setSelectedIndex:(NSUInteger)selectedIndex {

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UINavigationController *navigationController = [originalViewController as:[UINavigationController class]];
        if (navigationController.presentedViewController) {
            [navigationController dismissViewControllerAnimated:NO completion:^{
                [navigationController popToRootViewControllerAnimated:NO];
            }];
        }else if (navigationController.topViewController){
            [navigationController popToRootViewControllerAnimated:NO];
        }
    });

}

}

#1


16  

The view probably wasn't onscreen. It has to be onscreen (visible) for the viewWillDisappear: method to be called. If it's coming back from the background, it wasn't visible.

视图可能不在屏幕上。它必须在屏幕上(可见)才能调用viewWillDisappear:方法。如果它从后台返回,则不可见。

You could try using willMoveToParentViewController: which is called when the view controller is removed from its parent.

您可以尝试使用willMoveToParentViewController:当视图控制器从其父控件中删除时调用。

#2


0  

such useful to me

对我有用

[nav performSelector:@selector(popToRootViewControllerAnimated:) withObject:nil afterDelay:0.0];

I rewrote UITabBarController

我重写了UITabBarController

- (void)setSelectedIndex:(NSUInteger)selectedIndex {

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UINavigationController *navigationController = [originalViewController as:[UINavigationController class]];
        if (navigationController.presentedViewController) {
            [navigationController dismissViewControllerAnimated:NO completion:^{
                [navigationController popToRootViewControllerAnimated:NO];
            }];
        }else if (navigationController.topViewController){
            [navigationController popToRootViewControllerAnimated:NO];
        }
    });

}

}