現(xiàn)階段的iOS應(yīng)用開發(fā),我們必須在AppDelegate中設(shè)置self.window.rootViewController,但是在以前老版本的xcode中可以不設(shè)置,iOS應(yīng)用也可以照常運(yùn)行。因此如果一些老的項(xiàng)目或者照一些老的iOS教程書籍敲的代碼,運(yùn)行就可能會(huì)出現(xiàn)下面的異常:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'Application windows are expected to have a root view controller at the end of application launch'
并且app只能打開啟動(dòng)屏,無法進(jìn)入應(yīng)用,程序在main函數(shù)中停止運(yùn)行,如下圖所示:
norootviewcontroller.png
這里我碰到這個(gè)問題的demo是這樣的,有一個(gè)自定義view,代碼如下:
//
// BNRHypnosisView.h
// Hypnosister
//
// Created by JackRo on 2017/2/21.
// Copyright ? 2017年 JackRo. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface BNRHypnosisView : UIView
@end
//
// BNRHypnosisView.m
// Hypnosister
//
// Created by JackRo on 2017/2/21.
// Copyright ? 2017年 JackRo. All rights reserved.
//
#import "BNRHypnosisView.h"
@implementation BNRHypnosisView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (void)drawRect:(CGRect)rect {
CGRect bounds = self.bounds;
CGPoint center;
center.x = bounds.origin.x + bounds.size.width / 2.0;
center.y = bounds.origin.y + bounds.size.height / 2.0;
float radius = (MIN(bounds.size.width, bounds.size.height) / 2.0);
UIBezierPath *path = [[UIBezierPath alloc] init];
[path addArcWithCenter:center radius:radius startAngle:0.0 endAngle:M_PI*2.0 clockwise:YES];
[path stroke];
}
@end
AppDelegate中代碼如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CGRect firstFrame = self.window.bounds;
BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];
firstView.backgroundColor = [UIColor redColor];
[self.window addSubview:firstView];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
根據(jù)異常信息的意思,應(yīng)用就是缺少一個(gè)rootViewController,所以在AppDelegate中給應(yīng)用設(shè)置rootViewController就可以了,所以解決后的代碼如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//-----------------------------------------------------------------------------------------
//解決該問題的代碼
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow *window in windows) {
if(window.rootViewController == nil){
UIViewController *vc = [[UIViewController alloc]initWithNibName:nil
bundle:nil];
window.rootViewController = vc;
}
}
//解決該問題的代碼
//----------------------------------------------------------------------------------------
CGRect firstFrame = self.window.bounds;
BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];
firstView.backgroundColor = [UIColor redColor];
[self.window addSubview:firstView];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}