Attempt to present on whose view is not in the window hierarchy!
##
- (void)viewDidLoad {
[super viewDidLoad];
UIStoryboard *story = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
UIViewController *controller = [story instantiateViewControllerWithIdentifier:@"RootViewController"];
[self presentViewController:controller animated:YES completion:nil];
}
上面代码中的问题是在controller a的viewDidLoad里面直接跳转controller b,然后导致在显示controller b的时候也调用了controller a来显示,然后发现这个window的层次结构就错乱了。
参考:stackoverflow给出的回答
我的解决方案是:等到viewcontroller 显示完之后再跳转
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIStoryboard *story = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
UIViewController *controller = [story instantiateViewControllerWithIdentifier:@"RootViewController"];
[self presentViewController:controller animated:YES completion:nil];
}
本文详细阐述了一个在ViewController中直接跳转到另一个ViewController导致窗口层次结构混乱的问题,并提供了解决方案。通过将跳转操作从viewDidLoad移至viewDidAppear,避免了错误的层级显示。解决方案确保了应用程序的稳定性和用户体验。
1万+

被折叠的 条评论
为什么被折叠?



