如何在使用TabBarController时从AppDelegate调用视图控制器,即didFinishLaunchingWithOptions

时间:2015-12-25 22:49:37

标签: ios objective-c iphone xcode

我是iOS编程的新手,并且不太了解以编程方式调用ViewControllers。我正在制作笔记应用,我想让它受密码保护。我使用TabBarController,其中一个选项卡用于设置(用于启用和禁用密码保护)。当应用程序启动时,我想知道启用或禁用密码保护。如果启用,我想显示登录屏幕(即调用不同的视图控制器)。我认为我应该从AppDelegate中的didFinishLaunchingWithOptions方法做到这一点,但我不知道如何。我尝试了一些方法,但失败了。请帮帮我。

提前致谢。

1 个答案:

答案 0 :(得分:-1)

对不起投票表示抱歉 - 我认为你不值得。当我启动iOS时,我遇到了同样的问题。这可能会有所帮助

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //set NSDefaults when you set if password protection is needed - here you are checking to see if password enabling  exists

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL passwordExists;
    passwordExists = [defaults boolForKey:@"hasCompleted"];

    //jump to different view controller depending if BOOL is false or true
   //set names (ShowLogInScreenController) In StoryBoardID in the Interface Builder 
    UIStoryboard *storyBoard =[ UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UINavigationController *vc = [storyBoard instantiateViewControllerWithIdentifier:passwordExists ? @"ShowLogInScreenController" : @"PasswordProtectionDisabledViewController"];
    UINavigationController *questionsNavigationController = [[UINavigationController alloc] initWithRootViewController:vc];
    questionsNavigationController.navigationBar.hidden = YES;
    self.window.rootViewController = questionsNavigationController;
    return YES;
}