Warning: Attempt to present <UINavigationController: 0x6e8ade0> on <LoginViewController: 0x6b3ef90> whose view is not in the window hierarchy
错误提示:iOS 7.1环境下测试没问题,换到ios8环境下回报这个错误Warning: Attempt to present <UINavigationController: 0x6e8ade0> on <LoginViewController: 0x6b3ef90> whose view is not in the window hierarchy!
1. 代码逻辑是:
LoginViewController在登陆后会present到一个UINavigationController,在这个UINavigationController中,会有监听事件,监听到后会present到另外一个新的UINavigationController,这个错误就是跳转的时候引发的,我用的-(UIViewController *)getCurrentRootViewController这个函数来获取当前顶层的控制器,但是到ios8环境下,这个检测不到LoginViewController之后所有得viewcontroller,好像只能显示window的rootviewcontroller请问怎么解决
//获取当前viewcontroller
-(UIViewController *)getCurrentRootViewController {
UIViewController *result;
// Try to find the root view controller programmically
// Find the top window (that is not an alert view or other window)
UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
if (topWindow.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(topWindow in windows)
{
if (topWindow.windowLevel == UIWindowLevelNormal)
break;
}
}
UIView *rootView = [[topWindow subviews] objectAtIndex:0];
id nextResponder = [rootView nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]])
result = nextResponder;
else if ([topWindow respondsToSelector:@selector(rootViewController)] && topWindow.rootViewController != nil)
result = topWindow.rootViewController;
else
NSAssert(NO, @"ShareKit: Could not find a root view controller. You can assign one manually by calling [[SHK currentHelper] setRootViewController:YOURROOTVIEWCONTROLLER].");
return result;
}
1.解决方法:
看了一下你的代码,你不应该用 [self getCurrentRootViewController] 去present ,
因为当第一次present之后,此时window上面的顶级视图是 ffViewController ,这时候如果你继续present 需要 用 ffViewController 这个对象。
我大概理解你的想法是 想当前的controller 直接 模态
通过我下面的函数可以获取当前的controller ( [[self topViewController] presentViewController:[[UIViewController alloc] init] animated:YES completion:nil];)
- (UIViewController*)topViewController
{
return [self topViewControllerWithRootViewController:self.window.rootViewController];
}
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController
{
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabBarController = (UITabBarController *)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}