iOS问题解决(一):Application windows a
2017-02-22 本文已影响1976人
JackRo
现阶段的iOS应用开发,我们必须在AppDelegate中设置self.window.rootViewController,但是在以前老版本的xcode中可以不设置,iOS应用也可以照常运行。因此如果一些老的项目或者照一些老的iOS教程书籍敲的代码,运行就可能会出现下面的异常:
*** 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只能打开启动屏,无法进入应用,程序在main函数中停止运行,如下图所示:
norootviewcontroller.png
这里我碰到这个问题的demo是这样的,有一个自定义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;
}
根据异常信息的意思,应用就是缺少一个rootViewController,所以在AppDelegate中给应用设置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;
}