iOS:如何在现有的 UINavigationController 中打开另一个 UINavigationController?

标签 ios objective-c uinavigationcontroller

我正面临以下代码的崩溃。场景是

这是我的应用委托(delegate)方法,我在其中使用 UINavigationController 加载 RTC_HomeVC。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
RTC_HomeVC *obj_RTC_HomeVC=[[RTC_HomeVC alloc]init];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:obj_RTC_HomeVC];
// Override point for customization after application launch.
self.window.rootViewController=nav;
[obj_RTC_HomeVC release];
[nav release];

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

现在我想在父导航 Controller 中打开 UINavigationController。所以我使用以下代码。方法 -(IBAction)call_SectionFlow 在 RTC_HomeVC 中。


-(IBAction)call_SectionFlow{
RTC_1_StoreDetailsVC *obj_StoreDetailsVC=[[RTC_1_StoreDetailsVC alloc]initWithNibName:@"RTC_1_StoreDetailsVC" bundle:nil];
RTC_3_EnablingWorksVC *obj_EnablingWorksVC = [[RTC_3_EnablingWorksVC alloc]initWithNibName:@"RTC_3_EnablingWorksVC" bundle:nil];
UINavigationController *navController_Sections = [[UINavigationController alloc] init];
NSArray *array_ControllerArray=[[NSArray alloc]initWithObjects:obj_StoreDetailsVC,obj_EnablingWorksVC, nil];
[navController_Sections setViewControllers:array_ControllerArray animated:FALSE]
navController_Sections.view.frame=CGRectMake(14, 40, 996,636 );
[self.view addSubview:[[[navController_Sections viewControllers] objectAtIndex:0] view]];
}

当我调用此方法时,应用程序崩溃了。这是崩溃日志。

崩溃日志: * 由于未捕获的异常“UIViewControllerHierarchyInconsistency”而终止应用程序,原因:“ subview Controller : 应该有父 View Controller : 但实际父 View 是:” * 首先抛出调用栈: (0x1c9c012 0x10d9e7e 0x1c9bdeb 0x6838a 0x68739 0x6f5a3 0x67eed 0x4fc3 0x10ed705 0x24920 0x248b8 0xe5671 0xe5bcf 0xe4d38 0x543 3f 0x54552 0x323aa 0x23cf8 0x1bf7df9 0x1bf7ad0 0x1c11bf5 0x1c11962 0x1c42bb6 0x1c41f44 0x1c41e1b 0x1bf67e3 0x1bf6668 0x2165c 0x1f82 0x1c45) libc++abi.dylib:终止调用抛出异常


谁能告诉我

  1. 这段代码有什么问题?我应该采用哪种方法来解决此崩溃?
  2. 如何在现有的 UINavigationController 中打开另一个 UINavigationController?

谢谢。

最佳答案

不要手动将 subview 添加到UIWindow。它不受支持(或者至少它不能正常工作)。

使用这个方法:

[firstNavigationVC presentViewController:secondNavigationVC animated:YES completion:nil];

导致崩溃的原因是,您将 obj_StoreDetailsVC 添加到新的导航 Controller ,然后将其 View 添加到 self.view。一旦一个 VC 是另一个 VC 的 child ,它的 view 必须是那个 VC 的 view 的后代。也许您可以将 secondNavigationVC 的 View 添加到 firstNavigationVC 的 View 中,但这不是 UIKit 的工作方式。使用上面的方法。

关于iOS:如何在现有的 UINavigationController 中打开另一个 UINavigationController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14723355/

相关文章:

ios - 在呈现的 AVPlayerViewController 中循环 AVPlayerItem

ios - 如何从 NSObject 类获取导航 Controller 并在 ios 中推送 View Controller

ios - 为 UINavigationController 自定义状态栏背景

objective-c - 在 WebView Cocoa 中放大和缩小

ios - 在导航到另一个 View Controller 之前切换标签栏

iphone - NSPredicate 多字段搜索

ios - 如何在 UILabel 的 NSMutableArray 中移动 UILabel 的中心,取决于用户触摸并将 UILabel 拖动到的位置?

ios - 适用于 iOS 的 Facebook Photo Picker Controller(类似于 Places Picker Controller )

ios - 两个nsdates之间的时间差

objective-c - NSTreeController + 核心数据 : STOP KVO?