Enable and disable the tab bar item by a button click in xcode? Enable and disable the tab bar item by a button click in xcode? xcode xcode

Enable and disable the tab bar item by a button click in xcode?


I have to say that I am a beginner in iOS development bit I think I can help you.

In you Storyboard make a TabBarController and all the other UIViewController's. Link them to the TabBarController and add assign classes to them. In your case one of the UIViewController will be called LoginViewController.Now when your app starts the LoginViewController must be the first tab an you simply add this code to disable the tabs:

[[[[self.tabBarController tabBar]items]objectAtIndex:1]setEnabled:FALSE];[[[[self.tabBarController tabBar]items]objectAtIndex:2]setEnabled:FALSE];[[[[self.tabBarController tabBar]items]objectAtIndex:3]setEnabled:FALSE];

And again you can enable them with:

[[[[self.tabBarController tabBar]items]objectAtIndex:1]setEnabled:TRUE];[[[[self.tabBarController tabBar]items]objectAtIndex:2]setEnabled:TRUE];[[[[self.tabBarController tabBar]items]objectAtIndex:3]setEnabled:TRUE];

So your LoginAction function would look like this:

- (void) LoginAction {     AppDelegate *passData = (AppDelegate *)[[UIApplication sharedApplication] delegate];        if ([CustomerUsername.text isEqualToString:@""] || [CustomerPassword.text     isEqualToString:@""]) {        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Please Fill     all the field" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];        [alert show];        return;    }    // i will use a code from connect to DB tutorial    NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/Staff.php?userName=%@&password=%@",CustomerUsername.text, CustomerPassword.text];    // to execute php code    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];    // to receive the returend value    NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];    if ([strResult isEqualToString:@"1"]) {        //MainTabViewController *main = [[MainTabViewController alloc] initWithNibName:nil bundle:nil];        //UITabBarItem *tabBarItem = [[main.MainTabBar items] objectAtIndex:1];        //[tabBarItem setEnabled:TRUE];        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"You are now Logged In" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];        [alert show];        [[[[self.tabBarController tabBar]items]objectAtIndex:1]setEnabled:TRUE];        [[[[self.tabBarController tabBar]items]objectAtIndex:2]setEnabled:TRUE];        [[[[self.tabBarController tabBar]items]objectAtIndex:3]setEnabled:TRUE];        return;    }    else {        // invalid information        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Invalide Information" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];        [alert show];        return;    }}

I hope it helped :D


I updated the solution from @RonzyFonzy to work with N number of tab bar items:

 for (UITabBarItem *tmpTabBarItem in [[self.tabBarController tabBar] items])           [tmpTabBarItem setEnabled:NO];