有时,我的应用启动时出现黑屏,并出现以下警告:
Application windows are expected to have a root view controller at the end of application launch
我使用 Storyboard。当我编译应用程序时,这往往会不时随机发生,并且只有在我更改代码和/或 Storyboard然后重新编译时,错误才会消失。 错误的编译将继续以错误的状态启动应用程序,即使您重新启动它也是如此。
应用程序本身(对我来说)唯一不寻常的地方是它的初始 View 包含一个 map View ,上面有多个容器 View (在启动时隐藏)。
我试过使用 Spark Inspector发生这种情况时调试 View 层次结构,它表明唯一的元素是 UIWindow,根本没有加载任何 UIView。
这是我的 didFinishLaunchingWithOptions
(这是应用程序委托(delegate)中唯一包含代码的方法),如 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;
}
最佳答案
如果你正在使用 Storyboard,你可以从 Storyboard设置 Root View Controller
否则您必须在 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 - Root View Controller 偶尔丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25656998/