iOS: 为什么没有显示导航栏?

iOS: Why is the navigation bar not shown?

我的应用程序启动到登录视图或主菜单,具体取决于用户是否在上一个会话中登录。这就是我以编程方式定义 "initial view controller" 的原因。

登录场景是导航控制器的根视图控制器,用户可以从那里选择注册或登录。

问题是,如果使用以下代码,实际上会显示登录视图,但缺少导航栏...知道我做错了什么吗?如果我在 Interface-Builder 中将登录场景声明为 "initial view controller",一切正常,但我确实需要以编程方式显示视图。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"LoginScene"];
}

如果 LoginScene 是导航控制器的根视图控制器,那么如果我理解正确的话,您应该将导航控制器设置为 window 的根:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"LoginNavController"];
}