在didFinishLaunchingWithOptions方法中崩溃

时间:2012-10-25 01:40:31

标签: iphone ios appdelegate

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];

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

错误讯息:

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

仍然不知道

3 个答案:

答案 0 :(得分:0)

您没有明确指出您的问题,所以请在控制台上添加错误消息。没有错误消息我们无法分辨问题出在哪里。任何由于nibfile名称而导致崩溃的情况,您指定了nib文件名为nil.Please指定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;
    }

我希望这段代码可以解决您的问题

答案 1 :(得分:0)

你的逻辑错了。添加rootViewController或添加navigationController作为窗口的子视图。您不能同时添加两个viewcontrollers。您的navigationController将覆盖您的rootviewcontroller。如果可能,然后将rootviewcontroller添加到navigationController或将navigationController添加到rootviewcontroller

答案 2 :(得分: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];