- 在APP打開的時(shí)候就調(diào)用這個(gè)方法,有時(shí)候會(huì)失效
LoginVC *vc = [LoginVC new];
vc.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:vc animated:NO completion:nil];
a)、我們說說失效情況, 如果我們的跟視圖是self.window.rootViewController = rootVc
那么我們?cè)趓ootVc的viewDidLoad 中調(diào)用 [self presentViewController:vc animated:NO completion:nil]; 肯定是不成功的。
會(huì)報(bào)一個(gè)錯(cuò)誤(null)) whose view is not in the window hierarchy.
例如我們r(jià)ootVC 是 UINavigationController或者是 UITabBarController、UIViewController,那么我們?cè)谒麄兊?code>viewDidLoad方法中調(diào)用[self presentViewController:vc animated:NO completion:nil];就會(huì)失效
b)、正確的使用情況, 如果我們的跟視圖是self.window.rootViewController = rootVc,
rootVc 是UINavigationController,那我們?cè)?code>UINavigationController 的 rootViewController 的 viewDidLoad 方法中調(diào)用 [self presentViewController:vc animated:NO completion:nil]; ,就能夠present 成功
同理 rootVc 是UITabBarController,那我們?cè)?code>UITabBarController 的 first ChildViewController 的 viewDidLoad 方法中調(diào)用 [self presentViewController:vc animated:NO completion:nil]; ,就能夠present 成功
2.在APP打開的時(shí)候找不到UIWindow
NSLog(@" ---- %@",[UIApplication sharedApplication].keyWindow);
還是用上面的問題,替換[self presentViewController:vc animated:NO completion:nil];代入就好