我正在做Facebook集成教程,我想显示我的MainViewController,如果用户拥有当前状态的有效令牌,否则我想显示LoginViewController.

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.

推荐答案

我也有同样的问题.根据this question的答案,我在presentViewController:animated:completion:之前加了[self.window makeKeyAndVisible],这为我修正了它.

在您的情况下,showLoginView将成为

- (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];
}

Objective-c相关问答推荐

如何使用 iOS 7 SpriteKit 粒子向非游戏的 iOS 应用程序添加粒子效果?

UIAlertView 按钮操作

表达式不可分配 - 将浮点数分配为 Xcode 中其他两个浮点数的总和时出现问题?

如何在旋转时定义 CollectionView 的大小

获取 Objective-C 类或实例的所有方法

如何将 NSMutableArray 中的字符串按字母顺序排序?

NSTimeInterval 格式

负数的奇怪 Objective-C Mod 行为

在 UILabel.attributedText *not* 蓝色和 *not* 下划线

如何正确获取文件大小并将其转换为 Cocoa 中的 MB、GB?

切换隐私设置将杀死应用程序

[Facebook-iOS-SDK 4.0]如何从FBSDKProfile获取用户邮箱

使用 UIView animateWithDuration 进行动画处理时无法 UIButton

如何删除 xcode 5 中的旧配置文件?

访问当前视图的父视图 - iOS

如何以编程方式移动 UIScrollView 以集中在键盘上方的控件中?

在 Objective C 中,你能判断一个对象是否具有特定的属性或消息吗?

我可以在 Objective-C 中重载运算符吗?

如何将 tintColor 应用于 UIImage?

将参数传递给由 NSTimer 调用的方法