ios 尝试在视图不在窗口层次结构中的 * 上显示 *

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13350938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:11:27  来源:igfitidea点击:

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

objective-cioscocoa-touchios6

提问by patryk

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:

我正在尝试在我的应用程序委托中创建一个模态视图控制器(我创建了一个名为 showLoginView 的函数)。但是每当我尝试调用它时,我都会在 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?

如何将视图添加到窗口层次结构中?或者我做错了什么?

采纳答案by HackyStack

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...

您无法从 appDelegate 显示模态视图控制器。您需要从当前全屏显示的视图控制器中显示模式视图控制器。换句话说,您需要将该代码放入您的根视图控制器中,或者您想从中显示模态 vc 的任何一个...

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

此外,您还需要使用“presentModalViewController”方法来呈现模态。您可以在模态 vc 上设置属性,例如:

vC.modalPresentationStyle = UIModalPresentationFormSheet;
vC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:vC animated:YES];

回答by Erwan

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.

实际上,您可以从 AppDelegate 呈现一个模态视图控制器,只要您检测到当前可见的 viewController 并注意当前控制器是导航控制器的情况。

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];

回答by SFeng

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];

回答by David Ganster

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

我在 iOS 7 上遇到了这个问题 - 使任何建议的解决方案起作用的关键是调用

[self.window makeKeyAndVisible];

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

在您的AppDelegate. 在那次调用之后,从窗口呈现一个模态视图rootViewController

回答by Titus T

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 并使用这个实例来调用presentViewController:

UIViewController *root = [UIApplication sharedApplication].keyWindow.rootViewController;
while (root.presentedViewController) {
    root = root.presentedViewController;
}

回答by Sanbrother

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

你可以 NSLog(@"%@", self.window.rootViewController),看看 rootViewController 到底是什么。

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

我遇到了这个问题,当 rootViewController 是一个普通的 UIViewController 时。用 UINavigationController 替换它,希望它会有所帮助。

回答by IsPha

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 !

希望这可以帮助 !