【问题标题】:Warning: attempt to present ViewController whose view is not in the window hierarchy警告:尝试呈现视图不在窗口层次结构中的 ViewController
【发布时间】:2013-02-23 14:59:26
【问题描述】:

在我的一个应用程序中,我从 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];  
}

【问题讨论】:

  • 什么时候调用 didReceiveLocalNotification?
  • @Bhargavi :当收到本地通知时,它会被调用。

标签: ios objective-c xcode uiviewcontroller


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    [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 解决了这个问题?因为直到下一次运行循环才会调用选择器。所以事情有时间安定下来,你会跳过一个运行循环。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 是的 x2:将我的 presentView 移动到 viewDidAppear 解决了我的问题。
      【解决方案4】:

      这可能是因为当 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];
      }
      

      【讨论】:

      • 感谢您的建议。我试过这个。第一次运行良好,如果通知再次出现,它会显示相同的警告...:(
      【解决方案5】:

      我遇到了类似的问题,当应用程序进入前台时,我从 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 视图控制器。

      【讨论】:

        【解决方案6】:

        边缘案例:

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

        【讨论】:

          【解决方案7】:

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

          希望这会对您有所帮助。 万事如意!!!

          【讨论】:

            【解决方案8】:

            对于 Swift 2.0,我使用了 viewDidAppear 的覆盖:

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

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

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

            【讨论】:

              【解决方案9】:
              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];
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2019-12-25
                • 1970-01-01
                • 1970-01-01
                • 2017-07-18
                • 2020-08-07
                相关资源
                最近更新 更多