问题
I'm currently working on an app that will be using multiple story boards as follows:
1) login.storyboard (handles registration and login)
2) main.storyboard (handles game options and selection)
3) settings.storyboard (handles settings for game(s))
4) game.storyboard (actualy game play)
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];
}
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;
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?
Thanks in advance.
J
回答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.)
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.
回答2:
I tend to modally present login VCs. I always load the main VC. If no session token exists then I modally present the login VC without animation. Effectively this makes it appear as if the login view was present on startup. Once the user successfully logs in you can dismiss with the typical modal dismiss (fall off the bottom of the screen) or do something like a cross-fade.
For storyboards, I personally use multiple. If you have a non-trivial app the number of views in an SB can grow quite large. It can be difficult to manage - finding the one you want in the sea of views is annoying and time consuming. Further, it bogs my machine down horribly. Perhaps most importantly, if you are working on a team with source control (which you absolutely should be using) - it is really annoying to try to manage access to the one monolithic SB. Generally it isn't possible to merge changes if multiple devs modify a single SB. At least with multiple you can assign different people to different tasks which are each tied to their own SB. There's no performance advantage to having multiple SBs (only the instantiated views occupy memory). But there are advantages from a development efficiency point of view.
来源:https://stackoverflow.com/questions/21066508/best-way-to-use-multiple-storyboards-in-app