2012-10-25 60 views
0
self.rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; 
[self.window addSubview:self.rootViewController.view]; //App will not crash without this line 

self.navigationController = [[UINavigationController alloc] initWithRootViewController: self.rootViewController]; 
[self.window addSubview:self.navigationController.view]; 

我在模拟器中运行它,它为什么会崩溃?didFinishLaunchingWithOptions方法崩溃

错误消息:

Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', 
reason: 'adding a root view controller <RootViewController: 0x6871620> as a child of 
view controller:<UINavigationController: 0x6a86dc0>' 

仍然不知道

+0

可以显示功能'initWithNibName改变你的贴码:包:'你'RootViewController'? –

+0

,也请添加您的控制台上的错误消息。 –

+0

终止应用程序由于未捕获的异常“UIViewControllerHierarchyInconsistency”,理由是:“增加一个根视图控制器的视图控制器的子:” 仍然不知道 – JackieLam

回答

0

您还没有明确规定的问题,所以请添加错误消息,在console.Without错误消息,我们无法分辨得到了什么出现问题的地方。如果由于nibfile名称而导致崩溃,则将nib文件名指定为nil。请指定nib文件名。试试这个代码一次。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     // Override point for customization after application launch. 
     self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; 
     self.navigationController=[[UINavigationController alloc] initWithRootViewController:self.rootViewController]; 
     self.window.rootViewController = self.navigationController; 
    // [self.window addSubview:navigationController.view]; 
     [self.window makeKeyAndVisible]; 
     return YES; 
    } 

我希望这个代码将解决您的问题

0

你的逻辑是错误的。可以添加rootViewController或将navigationController添加为窗口的子视图。您不能同时添加两个视图控制器。这里你的navigationController会覆盖你的rootviewcontroller。如果可能的话再加入你的RootViewController的到navigationController或添加navigationController到RootViewController的

+0

THX了很多,真正有用的 – JackieLam

+0

没关系。投票答复并检查我的答案下面的标记。 –

0

由于要设置RootViewController笔尖名nil我希望你在管理你的RootViewController.m视图内法-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil或其他方式。

解决您的错误消息,你已经给,按以下方式

self.rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; 
self.navigationController = [[UINavigationController alloc] initWithRootViewController: self.rootViewController]; 
self.window.rootViewController = self.navigationController; 
[self.window makeKeyAndVisible]; 
相关问题