我的应用要求用户在第一次使用时注册。注册流程包含用户需要完成的三个屏幕。
用户完成注册流程后,进入应用的主界面。
用 Storyboard处理流程变化的最佳方式是什么?
我最好的猜测是使用两个 Storyboard,一个主 Storyboard和一个注册 Storyboard,然后在运行时检查是否需要注册流程并加载注册 Storyboard, 否则加载主 Storyboard。
这是处理两个流程的最佳或最常用方法吗?如果是,我如何在注册流程完成后切换到主 Storyboard?
最佳答案
您可以将所有内容都放在一个 Storyboard中,而且效果会很好。您只需选择要在应用程序启动期间实例化的 View Controller 。
首先,您需要转到目标的设置,并从显示“主界面”的位置删除 Storyboard。然后,您需要在应用委托(delegate)的 application:didFinishLaunchingWithOptions:
方法中使用如下代码:
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *viewController = nil;
BOOL needsSignUp = //Your check for whether you need to sign in or not here
if (needsSignUp) {
viewController = [storyboard instantiateViewControllerWithIdentifier:@"SignUpScreen"];
}
else {
viewController = [storyboard instantiateViewControllerWithIdentifier:@"MainAppScreen"];
}
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
这只是在您手动实例化 Storyboard后决定从 Storyboard中手动实例化哪个 View Controller 。您需要将 @"SignUpScreen"
和 @"MainAppScreen"
替换为 Storyboard中 View Controller 的实际标识符。
确保按照第一步操作,从目标的“主界面”中删除 Storyboard ,因为这是它自动实例化 Storyboard 并自动选择初始 View Controller 的原因,而您不再希望它这样做。
编辑:
每个 View Controller 都有对其实例化的 Storyboard的引用,因此当您完成注册并想要显示您的主应用程序屏幕时,您只需调用以下行即可获取它:
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"MainAppScreen"];
如果您使用 UINavigationController
来显示您的注册屏幕,您可以通过执行以下操作来替换 View Controller 堆栈:
[self.navigationController setViewControllers:@[viewController] animated:YES];
如果您想将主屏幕 View Controller 设置为 Root View Controller ,或将任何其他 View 设置为 Root View Controller ,您可以这样做:
self.view.window.rootViewController = viewController;
关于ios - 具有两个独立流程的 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21049286/