AppDelegate, rootViewController and presentViewController

前端 未结 6 2105
南方客
南方客 2020-11-27 15:38

I\'m doing the Facebook integration tutorial, I want to show my MainViewViewController if the user has a valid token for the current state otherwise I want to show LoginView

6条回答
  •  隐瞒了意图╮
    2020-11-27 16:12

    Sometimes presenting modal view controller from window.rootViewController may produce the same warning & have no effect. Example of such hierarchy of view controllers:

    1. [MYUITableViewController] (presented modally by MYUIViewController )
    2. [MYUIViewController] (rootViewController of UINavigationController below)
    3. [UINavigationController] (root)

    Now calling

    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:[UIViewController new] animated:YES completion:nil];
    

    will cause this exact warning (tested both on iOS6 & 7 Sim)

    Solution: Instead of using rootViewController - use the top one presented by it:

        UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
        while (topRootViewController.presentedViewController) 
        {
            topRootViewController = topRootViewController.presentedViewController;
        }
    
        [topRootViewController presentViewController:yourController animated:YES completion:nil];
    
    • Sometimes keyWindow may have been replaced by window with nil rootViewController (showing UIAlertViews, UIActionSheets on iPhone, etc), in that case you should use UIView's window property.

提交回复
热议问题