locking tab bar items and unlocking via login page

45 Views Asked by At

Hey guys having trouble with the UI tab controller,

So I have a login screen as my first controller I was wondering if there is a way to lock tab bar items and then unlocking them after login is successful?

At the moment I'm using NSDictionary to store my username and passwords and an UIAlertView for alerting whether the login is successful or not, but there really is no point of a login if tabs are accessible without the logging in.

below is my current code for logging in

 - (IBAction)loginNow {
    if ([[credentialsDictionary objectForKey:usernameField.text]isEqualToString:passwordField.text]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Correct" message:@"You have successfully logged in." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
        usernameField.hidden = YES;
        passwordField.hidden = YES;
        goButton.hidden = YES;
        welcomeField.hidden = NO;
        nameField.hidden = NO;

    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You have entered an incorrect Username or Password" message:@"Please try again." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
    }  
}
1

There are 1 best solutions below

0
On

You can use the tabBarController:shouldSelectViewController: method in UITabBarControllerDelegate protocol to return NO for tabs which should be inaccessible.

Set the relevant object (probably your main view controller) as the delegate of the tab bar, and implement the above method, returning NO when the user is not logged in and tries to access a tab which they should not have access to.