31

在我的一个应用程序中,我从该application didReceiveLocalNotification方法调用 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];  
}
4

9 回答 9

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];
}
于 2013-03-12T11:51:51.120 回答
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];

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

于 2013-11-20T01:41:36.147 回答
14

根据我的假设,我感觉您正试图myViewself.viewController以前呈现的self.viewController内容附加或放置在窗口层次结构中。因此,请确保在出现/附加到窗口myView后呈现。self.viewController

为什么模态视图控制器不能在 viewDidLoad 中显示另一个?

于 2013-03-08T06:13:54.947 回答
2

这可能是因为当出现 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];
}
于 2013-03-08T06:04:43.720 回答
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];
        }
    }
}

这没有问题,因为它首先关闭视图控制器(无论已将哪些视图推送到它上面)如果它在呈现登录视图控制器之前存在。如果没有,那么我可以直接显示登录视图控制器。

于 2013-10-09T11:59:54.070 回答
1

边缘案例:

有些人可能会通过 Google 找到这一点,值得指出的是,如果您将视图作为根视图控制器加载,而该视图未标记为 Storyboard 中的初始视图(另一个视图是),那么有时会引发此错误,然后您在顶部加载另一个视图。applicationdidfinish...例如,在从App Delegate的方法中以编程方式加载的登录视图中。只需将初始视图(拖动 Storyboard 中的箭头)更改为适合您的层次结构的视图。深奥,含糊,但值得指出。

于 2014-10-22T18:42:52.137 回答
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]; }

希望这会帮助你。祝一切顺利 !!!

于 2013-03-08T06:03:10.330 回答
0

因为Swift 2.0我使用了覆盖viewDidAppear

让 vc = self.storyboard?.instantiateViewControllerWithIdentifier("Second") as!第二视图控制器

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

其中“Second”是身份检查器中 SecondViewController 的情节提要 ID。

于 2015-12-09T05:51:43.147 回答
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];
于 2016-06-03T07:35:21.950 回答