ios 警告:尝试呈现其视图不在窗口层次结构中的 ViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15287678/
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
Warning: attempt to present ViewController whose view is not in the window hierarchy
提问by Vinoy Alexander
In one of my apps, I'm calling a viewController from the application didReceiveLocalNotification
method. The page loads successfully, but it shows a warning as :
在我的一个应用程序中,我从该application didReceiveLocalNotification
方法调用了一个 viewController 。页面加载成功,但显示如下警告:
Warning: Attempt to present <blankPageViewController: 0x1fda5190> on
<ViewController: 0x1fd85330> whose view is not in the window hierarchy!
My code is as follows :
我的代码如下:
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
blankPageViewController *myView = [[blankPageViewController alloc]
initWithNibName:@"blankPageViewController" bundle: nil];
myView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.viewController presentViewController:myView animated:NO completion:nil];
}
采纳答案by Vinoy Alexander
Finally I solved that issue with the following code.
最后我用下面的代码解决了这个问题。
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.blankviewController = [[blankPageViewController alloc] initWithNibName:@"blankPageViewController" bundle:nil];
self.window.rootViewController = self.blankviewController;
[self.window makeKeyAndVisible];
}
回答by hashier
Put
放
[self.viewController presentViewController:myView animated:NO completion:nil];
into a function e.g.
变成一个函数,例如
- (void)yourNewFunction
{
[self.viewController presentViewController:myView animated:NO completion:nil];
}
and then call it like this:
然后像这样调用它:
[self performSelector:@selector(yourNewFunction) withObject:nil afterDelay:0.0];
The problem got described hereand why does this performSelector:withObject:afterDelay
fix this problem? Because the selector will not be called until the next run of the run loop. So things have time to settle down and you will just skip one run loop.
问题在这里得到了描述,为什么这可以performSelector:withObject:afterDelay
解决这个问题?因为直到下一次运行循环才会调用选择器。所以事情有时间安定下来,你只需跳过一个运行循环。
回答by βhargav?
As per my assumption, I am feeling like you are trying to present myView
from self.viewController
before self.viewController
is attached or placed in window hierarchy. So just make sure to present myView
after self.viewController
gets appear/attached to window.
根据我的假设,我觉得您正在尝试myView
从附加或放置在窗口层次结构中self.viewController
之前呈现self.viewController
。因此,请确保myView
在self.viewController
出现/附加到窗口后呈现。
Why can't a modal view controller present another in viewDidLoad?
回答by BhushanVU
it could be because the viewcontroller's view is not currently loaded in window hierarchy when VC is presented...
这可能是因为当呈现 VC 时,viewcontroller 的视图当前未加载到窗口层次结构中...
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UINavigationController *navigationController = (UINavigationController*) self.window.rootViewController;
rootViewController = [[navigationController viewControllers] objectAtIndex:0];
blankPageViewController *myView = [[blankPageViewController alloc] initWithNibName:@"blankPageViewController" bundle: nil];
[rootViewController presentModalViewController:myView animated:YES];
}
回答by Rob
I was having a similar issue, I was calling the presentViewController from within my AppDelegate.m when the application entered the foreground:
我遇到了类似的问题,当应用程序进入前台时,我从 AppDelegate.m 中调用 presentViewController:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSDate * lastActive = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastActive"];
double interval = [[NSDate date] timeIntervalSinceDate:lastActive];
NSLog(@"Time Interval: %f", interval);
if (interval > 900) { // interval is in seconds, so 900 seconds = 15 mins
Login *pres = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"login"];
UINavigationController *navi = ((UINavigationController*)self.window.rootViewController);
if (navi.presentedViewController) {
[navi dismissViewControllerAnimated:YES completion:^{
[navi presentViewController:pres animated:NO completion:nil];
}];
} else {
[navi presentViewController:pres animated:NO completion:nil];
}
}
}
This worked no problem as it dismisses the view controller first (regardless of what views have been pushed onto it) if it is present before presenting the Login view controller. If not then I can just present the Login view controller straight away.
这没有问题,因为如果它在呈现登录视图控制器之前存在,它首先关闭视图控制器(无论什么视图被推送到它上面)。如果没有,那么我可以直接显示登录视图控制器。
回答by LpLrich
Edge Case:
边缘情况:
Some people may find this through Google and it is worth pointing out that sometimes this error is raised if you are loading a view up as the root view controller which is not labelled as the initial view in Storyboard (& another view is) and then you load another view on top. Eg, in Log In Views which are loaded programmatically from within the applicationdidfinish...
method of the App Delegate. Simply change the initial view (drag the arrow in Storyboard) to the appropriate one for your hierarchy. Esoteric, vague, but worth pointing out.
有些人可能会通过 Google 发现这一点,值得指出的是,如果您将视图加载为根视图控制器,该视图未标记为 Storyboard 中的初始视图(另一个视图是),则有时会引发此错误在顶部加载另一个视图。例如,在applicationdidfinish...
App Delegate 方法中以编程方式加载的登录视图中。只需将初始视图(在 Storyboard 中拖动箭头)更改为适合您的层次结构的视图。深奥、模糊,但值得指出。
回答by Sarah Prata
For Swift 2.0
I used an override for viewDidAppear
with:
因为Swift 2.0
我使用了一个覆盖viewDidAppear
:
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("Second") as! SecondViewController
让 vc = self.storyboard?.instantiateViewControllerWithIdentifier("Second") 作为!第二个视图控制器
self.presentViewController(vc, animated: true, completion: nil)
Where "Second" is storyboard ID of the SecondViewController in the identity inspector.
其中“Second”是身份检查器中 SecondViewController 的故事板 ID。
回答by Suraj Mirajkar
I have used Navigation Controller and on which i have used a
ModalViewController to present a Popup(Present over current context).
From this Popup i am opening a ViewController Modally which
caused the Warning. So to resolve i did below change:
[self.presentedViewController performSegueWithIdentifier:@"PresentScreenSegueId" sender:sender];
回答by Yashesh
If your application type id UINavigationController then you can use.
如果您的应用程序类型 id UINavigationController 那么您可以使用。
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
blankPageViewController *myView = [[blankPageViewController alloc]
initWithNibName:@"blankPageViewController" bundle: nil];
myView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController presentViewController:myView animated:NO completion:nil]; }
Hope this will help you. All the best !!!
希望这会帮助你。祝一切顺利 !!!