iOS問題解決(一):Application windows are expected to have a root view controller at the end of application launch

現(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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.從網(wǎng)上下載了一個(gè)游戲的開源代碼,初次運(yùn)行就爆了如下錯(cuò)誤: 實(shí)際上這個(gè)錯(cuò)誤根xcode版本有關(guān):原因:在較新的x...
    wangjianjun0730閱讀 602評(píng)論 0 0
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,034評(píng)論 25 709
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評(píng)論 19 139
  • 剩兩集沒看,慢慢補(bǔ)...... 開頭一個(gè)大大的既視感,吉米因?yàn)楸痪日邔?duì)自己產(chǎn)生恐懼而感到迷惘,他認(rèn)為自己的裝扮讓別...
    十字路口霉少年閱讀 374評(píng)論 0 0
  • 原創(chuàng)文|蘇吉兒 想在你夢的畫面 成為耀眼的禮花 火光四濺 綻放在夜空 化成一道道漂亮的斑斕線 在黑暗里 你在線的這...
    蘇吉兒閱讀 209評(píng)論 0 0

友情鏈接更多精彩內(nèi)容