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