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

2019-01-07 14:26发布

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:

Warning: Attempt to present <PSLoginViewController: 0x1fda2b40> on <PSViewController: 0x1fda0720> whose view is not in the window hierarchy!

Here's the method code:

- (void)showLoginView
{
    PSLoginViewController *loginViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:@"PSLoginViewController"];
    [self.window.rootViewController presentViewController:loginViewController animated:NO completion:nil];
}

How can I add the view to the window hierarchy? Or maybe I'm doing something very wrong?

7条回答
聊天终结者
2楼-- · 2019-01-07 14:52

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.

查看更多
冷血范
3楼-- · 2019-01-07 14:54

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];
查看更多
相关推荐>>
4楼-- · 2019-01-07 14:55

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];
查看更多
趁早两清
5楼-- · 2019-01-07 14:58

Another reason for that warning can be that you want to present a view controller from an instance which is not the top most view controller.

So first you have to get the topmost UIViewController and using this instance to call presentViewController:

UIViewController *root = [UIApplication sharedApplication].keyWindow.rootViewController;
while (root.presentedViewController) {
    root = root.presentedViewController;
}
查看更多
Fickle 薄情
6楼-- · 2019-01-07 14:59

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.

查看更多
别忘想泡老子
7楼-- · 2019-01-07 14:59

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 !

查看更多
登录 后发表回答