断言失败 – ,

我只在iOS9中收到以下错误。

这是我的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; if ([[NSUserDefaults standardUserDefaults] objectForKey:@"login_dict"]) { if ([[NSUserDefaults standardUserDefaults] objectForKey:@"isLogout"] == nil || [[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogout"] integerValue]== 0) { self.loginDict = [[BaseViewController sharedInstance] removeNullFromDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:@"login_dict"]]; self.firstViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil]; } if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogout"] integerValue]== 1) { self.firstViewController = [[WelcomeViewController alloc] initWithNibName:@"WelcomeViewController" bundle:nil]; } NSLog(@"Userinfo = %@",self.loginDict); } else { self.firstViewController = [[WelcomeViewController alloc] initWithNibName:@"WelcomeViewController" bundle:nil]; } self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.navigationController = [[BufferedNavigationController alloc] initWithRootViewController:self.firstViewController]; //[window makeKeyAndVisible]; [self.window setRootViewController:self.navigationController]; } 

注意:这段代码在Xcode 6.4和iOS8中工作正常。

  Assertion failure in -[UIApplication _runWithMainScene:transitionContext:completion:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3505.16/UIApplication.m:3294 

我不得不从应用程序didFinishLaunchingWithOptions中删除这一行:

 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

那为我修好了

在这里我通过检查navigationController是否为零来得到解决scheme: –

 if (self.navigationController== nil) { self.navigationController = [[BufferedNavigationController alloc] initWithRootViewController:self.firstViewController]; } else { [self.navigationController setViewControllers:@[self.firstViewController] animated:NO]; } 

使用这一行解决了我的问题(iOS 10):

 [self.window setRootViewController:self.navigationController]; 

是(为更老的iOS和Xcode工作):

 [self.window addSubview:navigationController.view];