2013-03-08 79 views
30

在我的一个应用程序中,我从application didReceiveLocalNotification方法调用viewController。该页面加载成功,但它显示一个警告是:警告:尝试呈现其视图不在窗口层次结构中的ViewController

Warning: Attempt to present <blankPageViewController: 0x1fda5190> on 
<ViewController: 0x1fd85330> whose view is not in the window hierarchy! 

我的代码如下:

-(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]; 
} 
+0

当您调用didReceiveLocalNotification? – 2013-03-08 06:02:43

+0

@Bhargavi:它是一个当收到本地通知时被调用的解析器。 – 2013-03-08 06:08:32

回答

21

最后我解决了用下面的代码这个问题。

-(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]; 
} 
0

如果你的应用程序类型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]; } 

希望这会对你有帮助。 所有最好的!

2

这可能是因为视图 ​​- 控制的观点目前尚未在窗口加载层次提出了VC的时候......

-(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]; 
} 
+0

感谢您的建议。我试过这个。它第一次正常工作,如果通知再次出现,它显示相同的警告...... :( – 2013-03-08 06:19:37

+0

编辑答应该工作... – BhushanVU 2013-03-08 06:32:27

12

按我的假设,我感觉就像你正试图从之前提出self.viewControllermyViewself.viewController被附加或放置在窗口层次结构中。所以在self.viewController出现/连接到窗口后,请确保显示myView

Why can't a modal view controller present another in viewDidLoad?

+1

是......移动到viewDidAppear完全解决它对我来说! – 2013-09-11 18:11:13

2

我有一个类似的问题,我打电话从我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]; 
     } 
    } 
} 

这工作没有任何问题,因为它驳回如果视图控制器在呈现Login视图控制器之前存在,则首先查看控制器(无论视图是否已被压入)。如果不是的话,我可以直接提交登录视图控制器。

15

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

的问题得到了描述here?为什么这个performSelector:withObject:afterDelay解决这个问题?因为选择器在运行循环的下一次运行之前不会被调用。所以事情有时间安顿下来,你会跳过一个运行循环。

1

边缘情况:

有些人可能会通过谷歌找到这一点,它是值得指出的是,有时,如果您正在加载一个视图了作为不被标记为初始视图的根视图控制器引发此错误在Storyboard(另一个视图是&),然后在上面加载另一个视图。例如,在登录视图中,这些视图是通过App Delegate的applicationdidfinish...方法以编程方式加载的。只需更改初始视图(将故事板中的箭头拖动到适合您​​的层次结构的适当位置)。深奥,含糊,但值得指出。

0

对于Swift 2.0我用了一个覆盖为viewDidAppear有:

设VC = self.storyboard .instantiateViewControllerWithIdentifier( “二”)作为?! SecondViewController

self.presentViewController(vc, animated: true, completion: nil) 

其中“Second”是身份检查器中SecondViewController的故事板ID。

0
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]; 
相关问题