【发布时间】:2014-01-11 18:56:00
【问题描述】:
我目前正在开发一个将使用多个故事板的应用程序,如下所示:
1) login.storyboard(处理注册和登录)
2) main.storyboard(处理游戏选项和选择)
3) settings.storyboard(处理游戏设置)
4) game.storyboard(实际上是玩游戏)
我目前在 NSUserDefaults 中测试会话令牌,如果存在,则加载 main.storyboard 否则 auth.storyboard 使用:
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:
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 秒),有没有办法加快速度或改进代码以避免这种延迟?
提前致谢。
J
【问题讨论】:
-
此代码在应用程序的哪个位置?登录是否执行 Web 请求?是否有可能部分/全部延迟来自网络请求?
-
为什么要使用这么多故事板?您应该只为 iPhone 使用一个情节提要,如果需要,还应该为 iPad 使用一个情节提要。但不适用于应用程序内的每个部分...
-
是的,登录确实执行了 NSURLSession 请求。服务器响应几乎是立即的,并且几乎立即记录到 NSLog,所以我不认为这是瓶颈。
-
@thorb 登录功能和注册是一个很少需要的视图,因此在 90% 的时间都不需要时加载和保留这些视图似乎很浪费。
-
@RWSDevTeam 这不是故事板的工作方式。除非实际需要,否则不会“加载并保留”视图或视图控制器。故事板非常有效。不要过早优化。
标签: ios iphone objective-c uiviewcontroller uistoryboard