viewWillDisappear not called when calling popToRootViewControllerAnimated

╄→尐↘猪︶ㄣ 提交于 2020-01-12 06:54:26

问题


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

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.

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.

Searching on the internet has only given two possible reasons:

  • If using a UINavigationController as a subview, you must call view[Will/Did]Disappear yourself
  • Not calling the proper super methods

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?


回答1:


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.

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




回答2:


such useful to me

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

I rewrote 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];
        }
    });

}



来源:https://stackoverflow.com/questions/17954402/viewwilldisappear-not-called-when-calling-poptorootviewcontrolleranimated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!