如何使用“Splash”UIViewController有条件地重定向到另一个视图控制器?

时间:2012-01-15 22:49:30

标签: objective-c ios cocoa-touch ios5 uikit

我有一个“Splash screen”ViewController扩展UIViewController,在我的故事板中设置为初始应用VC。该控制器具有登录表单。

当应用程序启动时,在屏幕上显示任何内容之前,我希望此启动VC检查用户默认值以查看用户是否已登录。如果是这样,我希望启动VC重定向到应用程序的家庭VC,屏幕上显示所有内容。

如果用户未登录,我希望Splash VC完成加载,显示登录表单。

我将如何实施此功能?我会将所有这些检查放在init方法中吗?我很难在启动VC初始化方法中获取任何代码,因为某些原因这些方法不会被调用。

viewDidLoad方法中的代码运行正常,但在那里运行代码将无法尝试允许已登录用户将应用程序直接启动到主屏幕。

连连呢?提前谢谢。

1 个答案:

答案 0 :(得分:4)

我选择放置此逻辑的位置在应用程序委托的application didFinishLaunchingWithOptions:中。以下是它的外观:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

        //////////////////////////////////////////////
        // 1. do loading data etc. 
        // 2. check whether user is signed in or not
        //////////////////////////////////////////////

        if(already signed in)
        {
            dispatch_sync(dispatch_get_main_queue(), ^{
                [self.window.rootViewController performSegueWithIdentifier:@"segue identifier to home VC" sender:self.window.rootViewController]; 
            });
        }
        else
        {
            dispatch_sync(dispatch_get_main_queue(), ^{
                [self.window.rootViewController performSegueWithIdentifier:@"segue identifier to login VC" sender:self.window.rootViewController];                 
            });
        }            
    });
    return YES;
}

这是我帮助代码的快速故事板。希望你明白了。

Storyboard for the splash-login, splash-home