简体   繁体   English

didFinishLaunchingWithOptions方法崩溃

[英]Crash in the didFinishLaunchingWithOptions method

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

I run it in the simulator and it crash, why? 我在模拟器中运行它,然后崩溃了,为什么?

Error message: 错误信息:

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

still have no idea 还是不知道

You are not specifying your problem clearly,so please add error message what you got on the console.Without error message we are unable to tell where the problem occur.Any how it crashes due to nibfile name,you specifying nib file name as nil.Please specify nib file name. 您没有明确指定问题,因此请添加错误消息,您在控制台上会得到什么。没有错误消息,我们无法分辨问题出在哪里。由于nibfile名称而导致崩溃的任何原因,您将nib文件名指定为nil。请指定笔尖文件名。 Try this code once. 尝试一次此代码。

- (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;
    }

I hope this code will solve your problem 我希望这段代码能解决您的问题

yours logic is wrong. 你的逻辑是错误的。 Either add rootViewController or add navigationController as window's subview. 添加rootViewController或将navigationController添加为窗口的子视图。 You can't add two viewcontrollers at the same time. 您不能同时添加两个视图控制器。 Here your navigationController will overwrite your rootviewcontroller. 在这里,navigationController将覆盖您的rootviewcontroller。 If possible then add your rootviewcontroller into navigationController or add navigationController into rootviewcontroller 如果可能,请将您的rootviewcontroller添加到navigationController或将navigationController添加到rootviewcontroller

Since You are setting RootViewController nib name to nil i hope you are managing your view in RootViewController.m inside method -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil or some other way. 由于您将RootViewController笔尖名称设置为nil我希望您可以在RootViewController.m -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil或其他方法中管理视图。

to fix your error message you have given, change your posted code following way 要修复您给出的错误消息,请按照以下方式更改已发布的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM