Ios 警告:尝试显示其视图不在窗口层次结构中的ViewController
在我的一个应用程序中,我正在通过Ios 警告:尝试显示其视图不在窗口层次结构中的ViewController,ios,objective-c,xcode,uiviewcontroller,Ios,Objective C,Xcode,Uiviewcontroller,在我的一个应用程序中,我正在通过应用程序didReceiveLocalNotification方法调用viewController。页面加载成功,但会显示以下警告: Warning: Attempt to present <blankPageViewController: 0x1fda5190> on <ViewController: 0x1fd85330> whose view is not in the window hierarchy! 如果应用程序类型id
应用程序didReceiveLocalNotification
方法调用viewController。页面加载成功,但会显示以下警告:
Warning: Attempt to present <blankPageViewController: 0x1fda5190> on
<ViewController: 0x1fd85330> whose view is not in the window hierarchy!
如果应用程序类型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]; }
希望这对你有帮助。
祝你一切顺利 这可能是因为当显示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];
}
根据我的假设,我感觉您正试图在将
self.viewController
附加或放置到窗口层次结构之前,从self.viewController
显示myView
。所以只要确保在self.viewController
出现/附加到窗口后显示myView
最后,我用以下代码解决了这个问题
-(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];
}
我遇到了类似的问题,当应用程序进入前台时,我正在从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];
}
}
}
这并没有问题,因为它在显示登录视图控制器之前先解除视图控制器(无论推到视图控制器上的是什么视图)。如果没有,我可以直接显示登录视图控制器。Put
[self.viewController presentViewController:myView animated:NO completion:nil];
变成一个函数
- (void)yourNewFunction
{
[self.viewController presentViewController:myView animated:NO completion:nil];
}
然后这样称呼它:
[self performSelector:@selector(yourNewFunction) withObject:nil afterDelay:0.0];
问题已被描述,为什么会执行选择器:withObject:afterDelay
修复此问题?因为直到运行循环的下一次运行时才会调用选择器。因此,事情有时间解决,您只需跳过一个运行循环。边缘案例:
有些人可能会通过Google发现这一点,值得指出的是,如果您将一个视图作为根视图控制器加载,而该视图在故事板中没有标记为初始视图(另一个视图是),然后在顶部加载另一个视图,则有时会出现此错误。例如,在从App委托的
applicationdifinish…
方法中以编程方式加载的登录视图中。只需将初始视图(拖动故事板中的箭头)更改为适合层次结构的视图。深奥、模糊,但值得指出 对于Swift 2.0
我使用了一个覆盖viewdideappear
,包括:
让vc=self.storyboard?.instanceeviewcontrollerwhithidentifier(“第二”)为!第二视图控制器
self.presentViewController(vc, animated: true, completion: nil)
其中“Second”是identity inspector中Second ViewController的情节提要ID。当您调用didReceiveLocalNotification时?@Bhargavi:这是一个在收到本地通知时调用的代理服务器。感谢您的建议。我试过这个。第一次它工作正常,如果通知再次出现,它将显示相同的警告…:(是的x2:将我的presentView移动到ViewDidAspect解决了我的问题。
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];
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];