我在根视图中使用
SWRevealViewController
,并在自定义类中使用两个按钮创建自定义静态视图。 UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *navigationController = [storyBoard instantiateViewControllerWithIdentifier:@"historyGoodsView"];
SWRevealViewController *rootViewController = (SWRevealViewController *)[[[UIApplication sharedApplication] keyWindow]rootViewController];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[navController pushViewController:navigationController animated:true];
但是推送后看到的错误如下:
由于未捕获的异常“UIViewControllerHierarchyInconsistency”而终止应用程序,原因:“添加根视图控制器 SWRevealViewController:0x137629e60 作为视图控制器的子级:UINavigationController:0x137658b50” *** 首先抛出调用堆栈: (0x1844bc2d8 0x195ce00e4 0x1844bc218 0x188f976c0 0x188f975b0 0x188f8cb00 0x188f8c700 0x188fea070 0x1000e39d8 0x188f31404 0x188f 1a4e0 0x188f30da0 0x188f30a2c 0x188f29f68 0x188efd18c 0x18919e324 0x188efb6a0 0x184474240 0x1844734e4 0x184471594 0x18439d2d4 0 x18dbb36fc 0x188f62fac 0x100108218 0x19635ea08) libc++abi.dylib:以 NSException 类型的未捕获异常终止
你的错误是
nested
UINavigationController
,因为建议在嵌套时不要使用多个UINavigationController。
因此,只需仅使用您的
UIViewController
,而不是在 UINavigationcontroller
上使用 UINavigationcontroller
。
我不确定你的问题是什么,但这是我正在使用 swift 的版本,它可以工作。
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let viewController = StartVC()
let navigationController = UINavigationController(rootViewController: viewController)
window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
如果您只想从另一个视图控制器推入一个视图控制器,请按以下步骤操作:
var newProfileVC = ProfileVC()
newProfileVC.mainVCPointer = self
navigationController?.pushViewController(newProfileVC, animated: true)
问题是您正在尝试将
UINavigationController
实例推送到 UINavigationController
的另一个实例中。这会产生 UIViewController
层次结构的内部不一致。试试这个:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *yourNextViewController = (UIViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"historyGoodsView"];
SWRevealViewController *rootViewController = (SWRevealViewController *)[[[UIApplication sharedApplication] keyWindow]rootViewController];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[navController pushViewController:yourNextViewController animated:YES];