"Application windows are expected to have a root view controller at the end of application launch" 当 运行 项目 Xcode 7,iOS 9 时出错

"Application windows are expected to have a root view controller at the end of application launch" error when running a project with Xcode 7, iOS 9

运行宁函数后

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

发生崩溃:

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

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', `enter code here`reason: 'Application windows are expected to have a root view controller at the end of application launch'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000109377885 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000108df0df1 objc_exception_throw + 48
    2   CoreFoundation                      0x00000001093776ea +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x0000000108a42bb1 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
    4   UIKit                               0x000000010760e350 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2875
    5   UIKit                               0x000000010760b73f -[UIApplication workspaceDidEndTransaction:] + 188
    6   FrontBoardServices                  0x000000010b87fd7b FrontBoardServices + 163195
    7   FrontBoardServices                  0x000000010b880118 FrontBoardServices + 164120
    8   CoreFoundation                      0x00000001092a20f1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    9   CoreFoundation                      0x0000000109297eac __CFRunLoopDoSources0 + 556
    10  CoreFoundation                      0x0000000109297363 __CFRunLoopRun + 867
    11  CoreFoundation                      0x0000000109296d78 CFRunLoopRunSpecific + 488
    12  UIKit                               0x000000010760b091 -[UIApplication _run] + 402
    13  UIKit                               0x000000010760f79b UIApplicationMain + 171
    14  bbwc                                0x00000001037a9998 main + 344
    15  libdyld.dylib                       0x000000010a45ca05 libdyld.dylib + 10757
    16  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

这个项目是一个老项目,我应该怎么做才能使它构建并运行 with Xcode 7 and iOS 9?

根据您的错误信息:

Application windows are expected to have a root view controller at the end of application launch

这个 "old" 项目有多旧?如果超过几年,你还有吗:

[window addSubview:viewController.view];

您应该将其替换为:

[window setRootViewController:viewController];

您应该在您的应用

中设置每个 window 的根视图控制器 属性

XCODE 7 要求所有的Windows必须有一个rootViewController 您可以使用简单的:

UIViewController* vc = [[UIViewController alloc]initWithNibName:nil bundle:nil];
self.window.rootViewController = vc;

如果您只需要使用 UIWindow(对于任何教程中的简单示例 - Xcode 7 之前),它会很好用!

如果您已经在应用程序委托中设置了 self.window 的根 ViewController,但在运行时仍然出现此错误,那么您的 window 中可能有多个 window UIApplication 其中之一可能没有 rootViewController 关联。您可以遍历您的应用程序 windows 并将一个空的 viewController 关联到它的根 ViewController 以修复您遇到的错误。

这是一个循环遍历应用程序 windows 的代码,如果 window 缺失,则将空 ViewController 关联到根 ViewController。

NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow *window in windows) {
    NSLog(@"window: %@",window.description);
    if(window.rootViewController == nil){
        UIViewController* vc = [[UIViewController alloc]initWithNibName:nil bundle:nil];
        window.rootViewController = vc;
    }
}

更新:显然有一个 window 专用于状态栏,这通常会导致此问题。上面的代码应该修复这个错误。

我有一个旧项目在 iOS 8 中工作,但在 iOS 9 中不工作。如果您的主界面设置为 MainWindow.xib,请将其更新为故事板。这为我修复了它:

  1. 新建一个项目,Single View Application就可以了。
  2. 将 Main.storyboard 文件复制到您的项目,或者您可以创建自己的文件。
  3. 打开项目设置并将主界面设置为 Main.storyboard

只需将您的 rootViewController 设置为 navigationController,它是应用程序中的 UIViewController-delegate.rb 就像我下面的代码一样。我是 ruby 的新人,但希望这对您有所帮助...

rootViewController = UIViewController.alloc.init

@window.rootViewController = navigationController

今天这也困扰着我,我花了几个小时来修复它:我的应用程序在 "MainWindow.xib" 中有 window,完整的导航控制器和随附的根视图控制器,这些都是以正确的顺序自动实例化的, Xcode 6 和 iOS8。

在 iOS9 上,该应用程序从 AppStore 下载时仍然 运行 正常,但 用 Xcode 7 新建时 和运行 on iOS 9. 在应用委托执行其 applicationDidBecomeActive: 方法时,根视图控制器现在 加载,就像以前一样!这使得根视图控制器错过了对我的恢复状态代码的调用。

我通过在代码中自己实例化根视图控制器并显式地从 viewDidLoad 恢复其状态来解决这个问题。

似乎自 iOS 9.1(?) 或 Xcode 7.1 以来任何 UIWindowapplication(_:didFinishLaunchingWithOptions:) 需要在离开该方法之前设置 rootViewController

以前,在该方法中只有主要 window 设置 rootViewController 就足够了。现在任何 UIWindow 实例都需要有一个有效的 rootViewController 属性.

这里的罪魁祸首可能是您自己的代码,如果您使用 UIWindow 以及在此期间尝试初始化新 UIWindow 实例的任何其他第三方库(例如状态栏消息叠加等)。

注意:如果您没有在主 window 上设置 rootViewControler 或者您的情节提要未设置,您也会遇到同样的错误正确的。将此作为旁注提及,因为这些情况非常明显且易于修复。

我或多或少继承了一个应用程序时遇到了这个问题。在验证情节提要已正确设置为应用程序主界面并且情节提要有 RootViewController 后,我仍然遇到崩溃。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'

经过进一步调查后我发现,崩溃是由 - (void)applicationDidBecomeActive:(UIApplication *)application 中调用的某些视图逻辑 (SVProgressHud) 引起的。这似乎是 Xcode7 中的新行为,但据我所知,SVProgressHud 在故事板设置之前引用了 rootviewcontroller。最终将 SVProgressHud 更新为 2.0 修复了该错误。

Swift 2 个对我有用的解决方案:

在 AppDelegate -> didFinishLaunchingWithOptions 中插入下面的代码

self.window!.rootViewController = storyboard.instantiateViewControllerWithIdentifier("YourRootViewController") as? YourRootViewControllerClass