Correctly present conditional login screen at app startup with storyboards and split view controllers?

試著忘記壹切 提交于 2019-12-03 16:47:38

I had the exact same problem but after much searching, Duane's answer gave me some insight. His answer still flashes on the previous view but I solved the problem by using:

-(void)viewWillAppear:(BOOL)animated {

    // Check if user is already logged in
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    if ([[prefs objectForKey:@"log"] intValue] == 1) {
        self.view.hidden = YES;
    }
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    // Check if user is already logged in
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    if ([[prefs objectForKey:@"log"] intValue] == 1) {
        [self performSegueWithIdentifier:@"homeSeg3" sender:self];
    }
}

-(void)viewDidUnload {
    self.view.hidden = NO;
}

You have to set the window.hidden property to NO before you can add subviews:

UITabBarController* tc = (UITabBarController*) self.window.rootViewController;

// Present the log in view controller
self.window.hidden = NO; // the window is initially hidden
[tc presentViewController:logInViewController animated:NO completion:NULL];

This is how i solved it in SWIFT

override func viewWillAppear(animated: Bool) {
    let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()

    let isloggedIn = prefs.objectForKey("isLoggedIn") as? Bool
    if (isloggedIn != false) {
        self.view.hidden = true
    } else {
        self.view.hidden = false
    }
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)

    let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()

    let isloggedIn = prefs.objectForKey("isLoggedIn") as? Bool
    if (isloggedIn != false) {
        println("this should work")
        self.performSegueWithIdentifier("Login", sender: self)
    }
}

I was able to do this by having the first view controller presented to the user be the login screen. Then there's a segue connected to the next screen with a tabview or split view or whatever you want.

This first controller handles the defaults and login credentials and once thats all checked and verified, follows the segue...otherwise it shows the user the login and/or error and sits there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!