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

前端 未结 7 1173
半阙折子戏
半阙折子戏 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条回答
  • 2020-11-30 06:05

    Faced this issue while trying to present controller from the call of delegate of other controller . i.e : show search filter with delegate , once done back to my controller and receive data via the delegate then present controller , all I had to do is to dispatch the present code cause while in a delegate you're in another thread , that's why you're presenting on your view from main thread another controller from that other thread , so have to go back to main thread , just put the presenting code like this :

         dispatch_async(dispatch_get_main_queue(), ^{
    [self presentViewController:searchVC animated:true completion:nil];
     });
    

    Hope this helps !

    0 讨论(0)
  • 2020-11-30 06:06

    You can't display a modal view controller from the appDelegate. You need to display a modal ViewController from whichever viewController is currently displaying full-screen. In other words, you need to put that code into your root view controller, or whichever one you want to display the modal vc from...

    Also, you'll want to use the method "presentModalViewController" to present the modal. You can set properties on the modal vc such as:

    vC.modalPresentationStyle = UIModalPresentationFormSheet;
    vC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:vC animated:YES];
    
    0 讨论(0)
  • 2020-11-30 06:07

    I ran into this problem on iOS 7 - the key to making any of the proposed solutions work was to call

    [self.window makeKeyAndVisible];
    

    in your AppDelegate. After that call, presenting a modal view from the window's rootViewController worked.

    0 讨论(0)
  • 2020-11-30 06:08
    UIViewController *activeController = [UIApplication sharedApplication].keyWindow.rootViewController;
    if ([activeController isKindOfClass:[UINavigationController class]])
    {
       activeController = [(UINavigationController*) activeController visibleViewController];
    }
    else if (activeController.modalViewController)
    {
        activeController = activeController.modalViewController;
    }
    [activeController presentModalViewController:vc animated:YES];
    
    0 讨论(0)
  • 2020-11-30 06:19

    You can NSLog(@"%@", self.window.rootViewController), and see what the rootViewController really is.

    I came into this problem, when the rootViewController is a normal UIViewController. Replace it with a UINavigationController, wish it will help.

    0 讨论(0)
  • 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];
    
    0 讨论(0)
提交回复
热议问题