- 在APP打开的时候就调用这个方法,有时候会失效
LoginVC *vc = [LoginVC new];
vc.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:vc animated:NO completion:nil];
a)、我们说说失效情况, 如果我们的跟视图是self.window.rootViewController = rootVc
那么我们在rootVc的viewDidLoad 中调用 [self presentViewController:vc animated:NO completion:nil]; 肯定是不成功的。
会报一个错误(null)) whose view is not in the window hierarchy.
例如我们rootVC 是 UINavigationController或者是 UITabBarController、UIViewController,那么我们在他们的viewDidLoad方法中调用[self presentViewController:vc animated:NO completion:nil];就会失效
b)、正确的使用情况, 如果我们的跟视图是self.window.rootViewController = rootVc,
rootVc 是UINavigationController,那我们在UINavigationController 的 rootViewController 的 viewDidLoad 方法中调用 [self presentViewController:vc animated:NO completion:nil]; ,就能够present 成功
同理 rootVc 是UITabBarController,那我们在UITabBarController 的 first ChildViewController 的 viewDidLoad 方法中调用 [self presentViewController:vc animated:NO completion:nil]; ,就能够present 成功
2.在APP打开的时候找不到UIWindow
NSLog(@" ---- %@",[UIApplication sharedApplication].keyWindow);
还是用上面的问题,替换[self presentViewController:vc animated:NO completion:nil];代入就好









网友评论