【问题标题】:How to choose rootViewController for App launch depending on a condition?如何根据条件为 App 启动选择 rootViewController?
【发布时间】:2015-08-13 16:25:17
【问题描述】:

我有一个应用程序,在第一次安装应用程序时我会显示我的第一个视图控制器。第二次运行后,我不想再显示第一个视图。

我已经保存了一个设置值,它告诉我应用程序之前是否运行过,如果有,那么我调用 performSegueWithIdentifier。

我不能在 viewDidLoad 中调用它,因为这不起作用,但如果我在 viewDidAppear 中调用它,那么我会在一瞬间看到第一个视图。

在第一个视图被处理之前,我可以在某个地方调用它吗?

【问题讨论】:

    标签: ios iphone xcode segue


    【解决方案1】:

    您需要在您的AppDelegate.m 中执行此操作。

    检查该应用之前是否启动过,并相应地设置您的window rootViewController。

    -(void) setRootViewControllerByCheckingLoginStatus
    {
        BOOL isUserLogin = [[NSUserDefaults standardUserDefaults] boolForKey:@"IS_USER_LOGGEDIN"];
    
        if (isUserLogin) {
    
            // Set your controller if user has logged in.
            YourController *yourController = [[YourController alloc] init]; // Or YourController *yourController = [storyBoard instantiateViewControllerWithIdentifier:@"YourControllerStoryBoardID"];
            self.window.rootViewController = yourController;
        }
        else
        {
          //set other controller if user is not logged in. Or just let it be what it is by default.
        }
    }
    

    在你的-application: didFinishLaunchingWithOptions:中调用上述方法

    你就完成了。

    编辑您的评论..

    黑屏意味着你没有正确实例化你的 ViewController。不要使用

    [[YourController alloc] init];
    

    使用

     YourController *yourController = [storyBoard instantiateViewControllerWithIdentifier:@"YourControllerStoryBoardID"];
    

    【讨论】:

    • 嗨 Mayur,我已经实现了上述操作,但是在运行时出现黑屏,好像视图没有加载一样??
    • 它与故事板错误,并希望我坚持 UIStoryBoard。使用 self.storyboard 不起作用,我从哪里获得故事板的实例?
    • [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    猜你喜欢
    • 2017-12-12
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多