AppDelegate、rootViewController 和presentViewController [英] AppDelegate, rootViewController and presentViewController
问题描述
我正在做 Facebook 集成教程,如果用户具有当前状态的有效令牌,我想显示我的 MainViewViewController,否则我想显示 LoginViewController.
I'm doing the Facebook integration tutorial, I want to show my MainViewViewController if the user has a valid token for the current state otherwise I want to show LoginViewController.
MainViewAppDelegate.m
MainViewAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
// To-do, show logged in view
} else {
// No, display the login page.
[self showLoginView];
}
return YES;
}
- (void)showLoginView
{
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
LoginViewController* loginViewController = [mainstoryboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[self.window.rootViewController presentViewController:loginViewController animated:YES completion:NULL];
}
控制台错误:
Warning: Attempt to present <LoginViewController: 0xb492fd0> on <MainViewViewController: 0xb1bd820> whose view is not in the window hierarchy!
我不想使用 NavigationController.
I don't want to use a NavigationController.
推荐答案
我遇到了同样的问题.基于对这个问题的回答,我在 presentViewController:animated:completion:
之前添加了 [self.window makeKeyAndVisible]
,并为我修复了它.
I had the same issue. Based on the answer to this question, I added [self.window makeKeyAndVisible]
just before presentViewController:animated:completion:
, and that fixed it for me.
在你的情况下, showLoginView 变成
In your case, showLoginView becomes
- (void)showLoginView
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
LoginViewController *loginViewController = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[self.window makeKeyAndVisible];
[self.window.rootViewController presentViewController:loginViewController animated:YES completion:NULL];
}
这篇关于AppDelegate、rootViewController 和presentViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!