使用UITabBarController设计从AppDelegate呈现viewController [英] Present viewController from AppDelegate using UITabBarController design
问题描述
我的应用程序是使用 UITabBarController
设计的,我试图在应用程序代表的基础上(登录屏幕)呈现视图。当我使用以下代码时:
My app is designed using a UITabBarController
and im trying to present a view on top of that (a login screen) from the app delegate. When i use the following code:
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];
我收到错误警告:尝试出现< Login_ViewController:0x716fac0> on< UITabBarController:0x7165240>其视图不在窗口层次结构中!
,屏幕只是黑色。如何将 Login_ViewController
添加到窗口层次结构中?
I get the error Warning: Attempt to present <Login_ViewController: 0x716fac0> on <UITabBarController: 0x7165240> whose view is not in the window hierarchy!
and the screen is just black. How do i add Login_ViewController
to the window hierarchy?
推荐答案
你可以总是抓住当前的根视图控制器并使用它来呈现你的登录控制器。
You can always grab the current root viewcontroller and use it to present your login controller.
UIViewController *presentingController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[presentingController presentViewController:viewController animated:YES completion:nil];
此外,根据我希望登录界面的外观,我会使用登录控制器UIModalPresentationFormSheet。
In addition, depending on how I want my login screen to look, I will push the login controller using UIModalPresentationFormSheet.
viewController.modalPresentationStyle = UIModalPresentationFormSheet;
这篇关于使用UITabBarController设计从AppDelegate呈现viewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!