有条件地从AppDelegate的情节提要中的不同位置开始 [英] Conditionally start at different places in storyboard from AppDelegate

查看:92
本文介绍了有条件地从AppDelegate的情节提要中的不同位置开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个故事板,其中设置了有效的登录名和主视图控制器,主视图控制器是登录成功后用户可导航到的视图控制器. 我的目标是:如果身份验证成功(存储在钥匙串中),则立即显示主视图控制器;如果身份验证失败,则显示登录视图控制器. 基本上,我想在我的AppDelegate中做到这一点:

I have a storyboard set up with working login and main view controller, the latter is the view controller to which the user is navigated to when login is successful. My objective is to show the main view controller immediately if the authentication (stored in keychain) is successful, and show the login view controller if the authentication failed. Basically, I want to do this in my AppDelegate:

// url request & response work fine, assume success is a BOOL here
// that indicates whether login was successful or not

if (success) {
          // 'push' main view controller
} else {
          // 'push' login view controller
}

我知道方法performSegueWithIdentifier:但是该方法是UIViewController的实例方法,因此无法在AppDelegate中调用. 如何使用现有的情节提要板??

I know about the method performSegueWithIdentifier: but this method is an instance method of UIViewController, so not callable from within AppDelegate. How do I do this using my existing storyboard ??

情节提要的初始视图控制器现在是导航控制器,该控制器未连接任何东西.我使用了setRootViewController:区别,因为MainIdentifier是UITabBarController.这就是我的线条:

The Storyboard's initial view controller now is a navigation controller which isn't connected to anything. I used the setRootViewController: distinction because MainIdentifier is a UITabBarController. Then this is what my lines look like:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{        
    BOOL isLoggedIn = ...;    // got from server response

    NSString *segueId = isLoggedIn ? @"MainIdentifier" : @"LoginIdentifier";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
    UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:segueId];

    if (isLoggedIn) {
        [self.window setRootViewController:initViewController];
    } else {
        [(UINavigationController *)self.window.rootViewController pushViewController:initViewController animated:NO];
    }

    return YES;
}

欢迎提出建议/改进!

推荐答案

我假设您的情节提要被设置为主情节提要"(Info.plist中的键UIMainStoryboardFile).在这种情况下,UIKit将加载情节提要,并将其初始视图控制器设置为窗口的根视图控制器,然后再将application:didFinishLaunchingWithOptions:发送到AppDelegate.

I assume your storyboard is set as the "main storyboard" (key UIMainStoryboardFile in your Info.plist). In that case, UIKit will load the storyboard and set its initial view controller as your window's root view controller before it sends application:didFinishLaunchingWithOptions: to your AppDelegate.

我还假设情节提要中的初始视图控制器是导航控制器,您要将主视图或登录视图控制器推到该导航控制器上.

I also assume that the initial view controller in your storyboard is the navigation controller, onto which you want to push your main or login view controller.

您可以向窗口询问其根视图控制器,并向其发送performSegueWithIdentifier:sender:消息:

You can ask your window for its root view controller, and send the performSegueWithIdentifier:sender: message to it:

NSString *segueId = success ? @"pushMain" : @"pushLogin";
[self.window.rootViewController performSegueWithIdentifier:segueId sender:self];

这篇关于有条件地从AppDelegate的情节提要中的不同位置开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆