如何使用故事板呈现启动/登录视图控制器

dan*_*anh 4 iphone storyboard ios appdelegate

我没有一个明确的答案,以各种形式看到这个问题.我打算在这里问一下这个问题.我的应用程序需要在启动时工作... inits,一些网络调用,登录等.我不希望我的主视图控制器工作,直到完成.这样做的好方法是什么?

要求:

  • iOS5 +,故事板,ARC.
  • 在启动工作完成之前,主vc的视图不会出现.
  • 想要在启动工作完成后选择过渡样式到主vc.
  • 想在故事板中做尽可能多的布局.
  • 代码应该干净地包含在显而易见的地方.

dan*_*anh 13

编辑,20177月从第一次写作开始,我已经将我的练习改为将我的启动任务交给他们自己的视图控制器.在那个VC中,我检查启动条件,如果需要,提供"忙"UI等.根据启动时的状态,我设置窗口的根VC.

除了解决OP问题之外,这种方法还具有更好地控制启动UI和UI转换的额外好处.这是怎么做的:

在您的主故事板中,添加一个名为的新VC,LaunchViewController并将其设为应用程序的初始vc.为您的应用程序的"真实"初始vc提供一个标识符,如"AppUI"(标识符位于IB中的标识选项卡上).

确定作为主UI流程开始的其他vcs(例如,注册/登录,教程等),并提供这些描述性标识符.(有些人更愿意将每个流程保留在自己的故事板中.这是一个很好的做法,IMO).

另一个不错的可选想法:给你的应用程序的启动故事板的vc一个标识符(比如"LaunchVC"),这样你就可以抓住它并在启动时使用它的视图.这将在启动期间以及执行启动任务时为用户提供无缝体验.

这就是我的LaunchViewController样子....

@implementation LaunchViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // optional, but I really like this:
    // cover my view with my launch screen's view for a seamless start
    UIStoryboard *storyboard = [self.class storyboardWithKey:@"UILaunchStoryboardName"];
    UINavigationController *vc = [storyboard instantiateViewControllerWithIdentifier:@"LaunchVC"];
    [self.view addSubview:vc.view];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self hideBusyUI];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self showBusyUI];

    // start your startup logic here:
    // let's say you need to do a network transaction... 
    //
    [someNetworkCallingObject doSomeNetworkCallCompletion:^(id result, NSError *error) {
        if (/* some condition */) [self.class presentUI:@"AppUI"];
        else if (/* some condition */) [self.class presentUI:@"LoginUI"];
        // etc.
    }];
}

#pragma mark - Busy UI

// optional, but maybe you want a spinner or something while getting started
- (void)showBusyUI {
    // in one app, I add a spinner on my launch storyboard vc
    // give it a tag, and give the logo image a tag, too
    // here in animation, I fade out the logo and fade in a spinner
    UIImageView *logo = (UIImageView *)[self.view viewWithTag:32];
    UIActivityIndicatorView *aiv = (UIActivityIndicatorView *)[self.view viewWithTag:33];
    [UIView animateWithDuration:0.5 animations:^{
        logo.alpha = 0.0;
        aiv.alpha = 1.0;
    }];
}

- (void)hideBusyUI {
    // an animation that reverses the showBusyUI
}

#pragma mark - Present UI

+ (void)presentUI:(NSString *)identifier {
    UIStoryboard *storyboard = [self storyboardWithKey:@"UIMainStoryboardFile"];
    UINavigationController *vc = [storyboard instantiateViewControllerWithIdentifier:identifier];

    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    window.rootViewController = vc;

    // another bonus of this approach: any VC transition you like to
    // any of the app's main flows
    [UIView transitionWithView:window
                      duration:0.3
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:nil
                    completion:nil];
}

+ (UIStoryboard *)storyboardWithKey:(NSString *)key {
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *storyboardName = [bundle objectForInfoDictionaryKey:key];
    return [UIStoryboard storyboardWithName:storyboardName bundle:bundle];
}

@end
Run Code Online (Sandbox Code Playgroud)

下面的原始答案,虽然我更喜欢我目前的方法

让我们表达应用程序准备运行主要的vc与布尔值,如:

BOOL readyToRun = startupWorkIsDone && userIsLoggedIn;
Run Code Online (Sandbox Code Playgroud)
  1. 创建一个AppStartupViewController并将其放在app storyboard中.
  2. 不要拖动任何segue,并且不要让它成为盯着vc,只是让它漂浮在某个地方.
  3. 在storyboard中的vc属性检查器中,将其标识符设置为"AppStartupViewController".

在AppStartupViewController.m中,当满足readyToRun条件时,它可以解除自身:

self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;   // your choice here from UIModalTransitionStyle
[self dismissViewControllerAnimated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

现在,只要应用程序变为活动状态,它就可以检查是否准备好运行,并在需要时显示AppStartupViewController.在AppDelegate.h中

- (void)applicationDidBecomeActive:(UIApplication *)application {

    BOOL readyToRun = startupWorkIsDone && userIsLoggedIn;

    if (!readyToRun) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        AppStartupViewController *startupVC = [storyboard instantiateViewControllerWithIdentifier:@"AppStartupViewController"];

        [self.window.rootViewController presentViewController:startupVC animated:NO completion:nil];
        // animate = NO because we don't want to see the mainVC's view
    }
}
Run Code Online (Sandbox Code Playgroud)

这主要是答案,但有一个问题.不幸的是,在呈现AppStartupViewController之前,主vc被加载(没关系)并获得viewWillAppear:消息(不行).这意味着我们必须在MainViewController.m中传播一些额外的启动逻辑,如下所示:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if (readyToRun) {
        // the view will appear stuff i would have done unconditionally before
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这是有帮助的.