在应用程序中使用多个故事板的最佳方式 [英] Best way to use multiple storyboards in app

查看:29
本文介绍了在应用程序中使用多个故事板的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个将使用多个故事板的应用,如下所示:

I'm currently working on an app that will be using multiple story boards as follows:

1) login.storyboard(处理注册和登录)

1) login.storyboard (handles registration and login)

2) main.storyboard(处理游戏选项和选择)

2) main.storyboard (handles game options and selection)

3) settings.storyboard(处理游戏设置)

3) settings.storyboard (handles settings for game(s))

4) game.storyboard(实际玩游戏)

4) game.storyboard (actualy game play)

我目前在 NSUserDefaults 中测试会话令牌,如果存在,则加载 main.storyboard 否则使用 auth.storyboard:

I currently test for a session token in NSUserDefaults and if it exists, load the main.storyboard otherwise auth.storyboard using:

NSUserDefaults *tagDefaults = [NSUserDefaults standardUserDefaults];

if (![tagDefaults objectForKey:@"session_token"]) {

    NSLog(@"session token not found");
    NSString *storyboardId = @"nonauth";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"login" bundle:nil];
    UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:storyboardId];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = initViewController;
    [self.window makeKeyAndVisible];

} else {

    NSString *storyboardId = @"init";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"init" bundle:nil];
    UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:storyboardId];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = initViewController;
    [self.window makeKeyAndVisible];
}

从登录屏幕如果登录返回有效我使用它在我的 NSURLSession 完成处理程序中从 login.storyboard 切换到 main.storyboard:

from the login screen if the login return is valid I use this to switch from login.storyboard to main.storyboard in my NSURLSession completion handler:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"main" bundle:nil];
    UINavigationController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"main"];
    self.view.window.rootViewController = viewController;

我的两个问题是:A)这是实现这一点的正确方法吗?B) 在 main.storyboard 实际加载之前从 login.storyboard 执行切换后有相当长的延迟(延迟 20-30 秒),有没有办法加快速度或改进代码以避免这种延迟?

My two questions are: A) is this the correct way to implement this? B) There is a considerable delay after executing the switch from login.storyboard before main.storyboard actually loads (20-30 seconds of delay), is there a way to speed this up or improve the code to avoid this delay?

提前致谢.

J

推荐答案

  1. 不要更改根视图控制器.没有必要.一个应用程序应该在应用程序的整个生命周期都有一个根视图控制器.如果您想用视图控制器的视图完全替换界面,请呈现该视图控制器.呈现的视图控制器可以出现并在应用程序的其余生命周期中保持在那里.(其实,如果登录界面是比较少见的界面,那么把主界面作为主界面,在上面呈现登录界面会更有意义.)

  1. Do not change the root view controller. There is no need. An app should have one root view controller for the entire lifetime of the app. If you want to completely replace the interface with a view controller's view, present that view controller. A presented view controller can appear and just stay there for the rest of the lifetime of the app. (Actually, if the login interface is the rarer interface, it would make more sense to make the main interface the main interface and present the login interface on top of it.)

同样,没有必要同时使用登录故事板和主故事板.这些界面可以在同一个storyboard中,这样就不需要加载新的storyboard.

By the same token, there is no need to use both a login storyboard and a main storyboard. Those sets of interface can be in the same storyboard, so that there is no need to load a new storyboard.

这篇关于在应用程序中使用多个故事板的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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