我编写了一个登录页面,在我的应用程序委托(在方法 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 循环。
我无法理解我试图调用的视图控制器如何不在层次结构中,因为它不仅显示正确,而且还是层次结构上的唯一视图。
编辑 1:我将包括 AppDelegate 应用程序:didFinishLaunchingWithOptions: 方法的部分,我在其中创建和添加 VC 和 Navigation 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];