根视图控制器的视图不在 Window 层次结构中
Root View Controller's view not in the Window Hierarchy
我编写了一个登录页面,在我的应用程序委托中(在 application:didFinishLaunchingWithOptions 方法下)设置为我的根视图控制器
[self.window setRootViewController:vc];
其中 vc 是我的 HomeVC 的实例化。
此视图加载正确。我可以输入登录所需的所有数据,但只要我尝试加载另一个视图,我就会得到
Warning: Attempt to present <RegisterVC: 0x7d07f520> on
<HomeVC: 0x7d374830> whose view is not in the window hierarchy!
我尝试了一些技巧来确定我使用以下代码获得了最顶层的视图控制器:
- (UIViewController*) topMostController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
但是,正如我所怀疑的,它只返回当前视图控制器,它也是根视图控制器。它甚至从未进入 while 循环。
我无法理解我尝试调用的视图控制器如何不在层次结构中,因为它不仅显示正确而且在层次结构中也是 only 视图全部.
编辑 1:我将包含 AppDelegate application:didFinishLaunchingWithOptions:我在其中创建和添加 VC 和导航 VC 的方法部分。
UIViewController *vc = (HomeVC *)[[HomeVC alloc] initWithNibName:NSStringFromClass([HomeVC class]) bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
[self.navigationController setNavigationBarHidden:YES];
[self.window setRootViewController:vc];
//^^^This is the line I'm most suspicious of, is that correct?^^^
[self.window setBackgroundColor:[UIColor colorWithHexString:kDefaultBackgroundColor]];
[self.window makeKeyAndVisible];
你可以得到window的root view controller,应该是navigation controller,然后得到它的top view controller。
尝试像 this:if 你确定有 viewController.presentedViewController 然后发送 includeModal 为 YES。
- (UIViewController *)topmostViewControllerFrom:(UIViewController *)viewController
includeModal:(BOOL)includeModal
{
if ([viewController respondsToSelector:@selector(selectedViewController)])
return [self topmostViewControllerFrom:[(id)viewController selectedViewController]
includeModal:includeModal];
if (includeModal && viewController.presentedViewController)
return [self topmostViewControllerFrom:viewController.presentedViewController
includeModal:includeModal];
if ([viewController respondsToSelector:@selector(topViewController)])
return [self topmostViewControllerFrom:[(id)viewController topViewController]
includeModal:includeModal];
return viewController;
}
我编写了一个登录页面,在我的应用程序委托中(在 application:didFinishLaunchingWithOptions 方法下)设置为我的根视图控制器
[self.window setRootViewController:vc];
其中 vc 是我的 HomeVC 的实例化。
此视图加载正确。我可以输入登录所需的所有数据,但只要我尝试加载另一个视图,我就会得到
Warning: Attempt to present <RegisterVC: 0x7d07f520> on
<HomeVC: 0x7d374830> whose view is not in the window hierarchy!
我尝试了一些技巧来确定我使用以下代码获得了最顶层的视图控制器:
- (UIViewController*) topMostController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
但是,正如我所怀疑的,它只返回当前视图控制器,它也是根视图控制器。它甚至从未进入 while 循环。
我无法理解我尝试调用的视图控制器如何不在层次结构中,因为它不仅显示正确而且在层次结构中也是 only 视图全部.
编辑 1:我将包含 AppDelegate application:didFinishLaunchingWithOptions:我在其中创建和添加 VC 和导航 VC 的方法部分。
UIViewController *vc = (HomeVC *)[[HomeVC alloc] initWithNibName:NSStringFromClass([HomeVC class]) bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
[self.navigationController setNavigationBarHidden:YES];
[self.window setRootViewController:vc];
//^^^This is the line I'm most suspicious of, is that correct?^^^
[self.window setBackgroundColor:[UIColor colorWithHexString:kDefaultBackgroundColor]];
[self.window makeKeyAndVisible];
你可以得到window的root view controller,应该是navigation controller,然后得到它的top view controller。
尝试像 this:if 你确定有 viewController.presentedViewController 然后发送 includeModal 为 YES。
- (UIViewController *)topmostViewControllerFrom:(UIViewController *)viewController
includeModal:(BOOL)includeModal
{
if ([viewController respondsToSelector:@selector(selectedViewController)])
return [self topmostViewControllerFrom:[(id)viewController selectedViewController]
includeModal:includeModal];
if (includeModal && viewController.presentedViewController)
return [self topmostViewControllerFrom:viewController.presentedViewController
includeModal:includeModal];
if ([viewController respondsToSelector:@selector(topViewController)])
return [self topmostViewControllerFrom:[(id)viewController topViewController]
includeModal:includeModal];
return viewController;
}