Attempt to present * on * whose view is not in the window hierarchy

前端 未结 7 1228
半阙折子戏
半阙折子戏 2020-11-30 05:29

I\'m trying to make a modal view controller in my app delegate (I created a function called showLoginView). But whenever I try to call it I get a warning in XCode:



        
7条回答
  •  猫巷女王i
    2020-11-30 06:21

    You can actually present a modal view Controller from the AppDelegate as long as you detect the current visible viewController and take care of the case where you current controller is a navigationController.

    Here is what I do:

    UIViewController *activeController = [UIApplication sharedApplication].keyWindow.rootViewController;
    if ([activeController isKindOfClass:[UINavigationController class]]) {
       activeController = [(UINavigationController*) activeController visibleViewController];
    }
    [activeController presentModalViewController:loginViewController animated:YES];
    

提交回复
热议问题