有时,我的应用程序会以黑屏启动,并显示以下警告:

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

我使用情节提要。在我编译应用程序时,这往往会不定期地随机发生,并且只有当我对代码和/或情节提要进行更改然后重新编译时,错误才会消失。 错误的编译将继续在错误状态下启动应用程序,即使您重新启动它也是如此。

对我而言,唯一不寻常的事情是它的初始视图包含一个 map 视图,并在其顶部具有多个容器视图(在启动时隐藏)。

我尝试使用Spark Inspector调试这种情况下的视图层次结构,它表明唯一的元素是UIWindow,根本没有加载UIViews。

这是我的didFinishLaunchingWithOptions(这是应用程序委托中唯一包含代码的方法),就像Matt请求的一样:
- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [DDLog addLogger:[DDASLLogger sharedInstance]];
    [DDLog addLogger:[DDTTYLogger sharedInstance]];
    [[DDTTYLogger sharedInstance] setColorsEnabled:YES];
    UIColor *pink = [UIColor colorWithRed:(118 / 255.0)
                                    green:(214 / 255.0)
                                     blue:(84 / 255.0)
                                    alpha:1.0];
    [[DDTTYLogger sharedInstance] setForegroundColor:pink
                                     backgroundColor:nil
                                             forFlag:LOG_FLAG_INFO];

    #ifdef DEBUG
        DDLogInfo(@"Launching in debug mode");
    #else
        DDLogWarn(@"Launching in release mode");
    #endif

    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
    [[AFNetworkActivityLogger sharedLogger] startLogging];

    [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil]
        setFont:[UIFont fontWithName:@"OpenSans" size:14]];

    NSMutableDictionary *titleBarAttributes = [NSMutableDictionary
        dictionaryWithDictionary:
            [[UINavigationBar appearance] titleTextAttributes]];
    [titleBarAttributes setValue:[UIFont fontWithName:@"OpenSans" size:18]
                          forKey:NSFontAttributeName];
    [[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];

    return YES;
}

最佳答案

如果使用情节提要,则可以从情节提要设置根视图控制器

否则,您必须在AppDelegate中以编程方式创建它

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UINavigationController *rootViewController = [[UINavigationController alloc] initWithNibName:@"RootVC" bundle:nil];
    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];
    return YES;
}

关于ios - 根 View Controller 偶尔丢失,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25656998/

10-11 14:51