iOS问题解决(一):Application windows are expected to have a root view controller at the end of application launch

现阶段的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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.从网上下载了一个游戏的开源代码,初次运行就爆了如下错误: 实际上这个错误根xcode版本有关:原因:在较新的x...
    wangjianjun0730阅读 572评论 0 0
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,482评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,982评论 19 139
  • 剩兩集沒看,慢慢補...... 開頭一個大大的既視感,吉米因為被救者對自己產生恐懼而感到迷惘,他認為自己的裝扮讓別...
    十字路口霉少年阅读 352评论 0 0
  • 原创文|苏吉儿 想在你梦的画面 成为耀眼的礼花 火光四溅 绽放在夜空 化成一道道漂亮的斑斓线 在黑暗里 你在线的这...
    苏吉儿阅读 186评论 0 0