我正面临以下代码的崩溃。场景是
这是我的应用委托(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 :
谁能告诉我
- 这段代码有什么问题?我应该采用哪种方法来解决此崩溃?
- 如何在现有的 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/