我有一个故事板设置了正常的登录名和主视图控制器,后者是成功登录后用户导航到的视图控制器。我的目标是,如果身份验证成功(存储在钥匙串中),则立即显示主视图控制器;如果身份验证失败,则显示登录视图控制器。基本上,我想在AppDelegate中执行此操作:
// url request & response work fine, assume success is a BOOL here
// that indicates whether login was successful or not
if (success) {
// 'push' main view controller
} else {
// 'push' login view controller
}
我知道方法performSegueWithIdentifier:但是该方法是UIViewController的实例方法,因此无法从AppDelegate中调用。如何使用现有的情节提要板来完成此任务?
编辑:
情节提要的初始视图控制器现在是未连接任何东西的导航控制器。我使用了setRootViewController:区别,因为MainIdentifier是UITabBarController。这就是我的代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
BOOL isLoggedIn = ...; // got from server response
NSString *segueId = isLoggedIn ? @"MainIdentifier" : @"LoginIdentifier";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:segueId];
if (isLoggedIn) {
[self.window setRootViewController:initViewController];
} else {
[(UINavigationController *)self.window.rootViewController pushViewController:initViewController animated:NO];
}
return YES;
}
欢迎提出建议/改进!