【问题标题】:How to change the present view controller?如何更改当前的视图控制器?
【发布时间】:2012-09-22 00:14:34
【问题描述】:

我有一个登录视图控制器,它向服务器发出请求以进行身份​​验证。一旦成功,我希望当前视图控制器成为我的应用程序的主页选项卡视图控制器。我确实尝试使用 pushViewController:viewController 或 presentModalViewController 或dismissModalViewControllerAnimated。没有任何效果。您可以查看提供的屏幕截图以了解我的应用程序流程。

这里是一些示例代码:

- (IBAction)facebookLoginButtonClick:(id)sender {

// get the app delegate so that we can access the session property
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];

// this button's job is to flip-flop the session from open to closed
if (appDelegate.session.isOpen) {
    // if a user logs out explicitly, we delete any cached token information, and next
    // time they run the applicaiton they will be presented with log in UX again; most
    // users will simply close the app or switch away, without logging out; this will
    // cause the implicit cached-token login to occur on next launch of the application
    [appDelegate.session closeAndClearTokenInformation];

} else {
    if (appDelegate.session.state != FBSessionStateCreated) {
        // Create a new, logged out session.
        appDelegate.session = [[FBSession alloc] init];
    }

    // if the session isn't open, let's open it now and present the login UX to the user
    [appDelegate.session openWithCompletionHandler:^(FBSession *session,
                                                     FBSessionState status,
                                                     NSError *error) {
        // and here we make sure to update our UX according to the new session state
        [self dismissModalViewControllerAnimated:YES];
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil];
        MainViewTabBarController *viewController = (MainViewTabBarController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"mainPageTabBarId"];
        [[self navigationController] presentModalViewController:viewController animated:YES];
    }];
}

}

【问题讨论】:

  • 当您说它不起作用时,您的意思是按钮完全没有响应吗?你在做任何错误检查吗?记录语句以查看完成块是否返回有效的 FBSession?
  • 2 个箭头的左侧是否有东西指向您的视图控制器?看起来您有 2 个控制器都设置为初始控制器(我不认为这是不可能的)。
  • 按钮响应。但是应用崩溃了。
  • 对不起,我刚刚发现我已经按了两次控制器。顺便说一句,开发带有故事板登录屏幕的应用程序的最佳方法是什么?

标签: iphone ios navigation storyboard presentmodalviewcontroller


【解决方案1】:

找出解决方案。我应该更改应用程序委托中的 rootViewController,而不是推送模型视图控制器。它运作良好。感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 2014-05-17
    • 2019-07-03
    • 2019-05-30
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 2012-02-19
    相关资源
    最近更新 更多