使用UITabBarController设计从AppDelegate呈现viewController

我的应用程序是使用UITabBarController设计的,我试图在应用程序委托之上呈现一个视图(登录屏幕)。 当我使用以下代码时:

 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; tabBarController = [[UITabBarController alloc] initWithNibName:@"Main_TabBarController" bundle:nil]; self.window.rootViewController = tabBarController; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil]; Login_ViewController *lvc = [storyboard instantiateViewControllerWithIdentifier:@"Login_ViewController"]; [self.window.rootViewController presentViewController:lvc animated:YES completion:nil]; 

我收到错误Warning: Attempt to present on whose view is not in the window hierarchy! 屏幕只是黑色。 如何将Login_ViewController添加到窗口层次结构?

您始终可以获取当前的根视图控制器并使用它来呈现您的登录控制器。

 UIViewController *presentingController = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; [presentingController presentViewController:viewController animated:YES completion:nil]; 

此外,根据我希望登录屏幕的外观,我将使用UIModalPresentationFormSheet推送登录控制器。

 viewController.modalPresentationStyle = UIModalPresentationFormSheet; 

您正在使用两种不同的机制来创建UI。 您应该将标签栏控制器移动到故事板中。 实例化故事板时,它会使用新实例和第一个控制器作为根控制器覆盖您的窗口。

错误消息告诉您标签栏控制器的视图不在视图层次结构中,而不是相反。

我将创建一个控制器,其视图仅包含您的应用程序徽标,并且在此控制器内部确定是否需要转到登录屏幕(如果您有持久登录)。 然后从登录屏幕转换到标签栏控制器。

除非您加载的故事板不是主要故事板,否则您不需要手动加载它。 您应该能够将故事板设置为应用程序的主要故事板,iOS将自动加载它。