Attempt to present a UIViewController whose view is not in the window hierarchy

眉间皱痕 提交于 2019-12-08 16:54:23

问题


I have a multi-view application with the following hierarchy:

splash -> navigation controller -> table view controller -> settings view controller

Splash is the application entry point and therefore becomes the root view controller. When I try to add a tile to the band via an action on the settings view controller, I get a debugger warning:

application[1929:1000746] Warning: Attempt to present <MSBAddTileDialogViewController_iOS: 0x15f0575b0> on <SplashViewController: 0x15dd597b0> whose view is not in the window hierarchy!

This happens immediately after the call to MSBClient.tileManager addTile:completionHandler:. The call never returns, no error is generated.

Any suggestions on how to get around this?


回答1:


You will need to get the root view controller and perform a segue from that view controller. This can be quite frustrating to debug but there are some answers on here about this topic.

Here is some code that I have used to perform a segue from the root view controller to a screen when the app receives a push notification.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YourStoryboard" bundle:nil];

YourViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"viewcontrolleridentifier"];

UIViewController *top = [UIApplication sharedApplication].keyWindow.rootViewController;

[top presentViewController:viewController animated:YES completion: nil];

Heres the same code in swift:

let storyboard = UIStoryboard.init(name: "YourStoryboard", bundle: nil)

let viewController = storyboard.instantiateViewController(withIdentifier: "viewcontrolleridentifier")

let top = UIApplication.shared.keyWindow?.rootViewController

top?.present(viewController, animated: true, completion: nil)

Make sure you set the view controllers identifier in your storyboard.

EDIT* If the view controller you are accessing is embedded within a navigation controller you will need to amend the above code,

Objective C:

UIViewController *top = [self topMostController];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

[top presentViewController:navigationController animated:YES completion: nil];

Swift:

let top = UIApplication.shared.keyWindow?.rootViewController

let navigationController = UINavigationController.init(rootViewController: viewController)

top?.present(navigationController, animated: true, completion: nil)


来源:https://stackoverflow.com/questions/34378802/attempt-to-present-a-uiviewcontroller-whose-view-is-not-in-the-window-hierarchy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!