我有一个具有以下层次结构的多视图应用程序:
飞溅 – >导航控制器 – >表视图控制器 – >设置视图控制器
Splash是应用程序入口点,因此成为根视图控制器.当我尝试通过设置视图控制器上的操作向乐队添加磁贴时,我收到调试器警告:
application[1929:1000746] Warning: Attempt to present <MSBAddTileDialogViewController_iOS: 0x15f0575b0> on <SplashViewController: 0x15dd597b0> whose view is not in the window hierarchy!
这在调用MSBClient.tileManager addTile:completionHandler:之后立即发生.调用永不返回,不会生成错误.
关于如何解决这个问题的任何建议?
解决方法
您需要获取根视图控制器并从该视图控制器执行segue.这对于调试来说非常令人沮丧,但是这里有关于这个主题的一些答案.
以下是我在应用程序收到推送通知时用于从根视图控制器执行segue到屏幕的一些代码.
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];
在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)
确保在故事板中设置视图控制器标识符.
编辑*如果您正在访问的视图控制器嵌入在导航控制器中,您将需要修改上述代码,
目标C:
UIViewController *top = [self topMostController]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; [top presentViewController:navigationController animated:YES completion: nil];
迅速:
let top = UIApplication.shared.keyWindow?.rootViewController let navigationController = UINavigationController.init(rootViewController: viewController) top?.present(navigationController,completion: nil)