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

user1580957 picture user1580957 · Sep 22, 2012 · Viewed 21.8k times · Source

I have 5 tab bar items. The first one will be the login page. When the user haven't logged on other tab bat items will be disabled, but when the user logged by clicking the navigationItem button all the other 4 tab bat items will be enabled.

I have made searched and found nothing... :(

Here's my code:

MainTabViewController.h
#import <UIKit/UIKit.h>

@interface MainTabViewController : UITabBarController
@property (retain, nonatomic) IBOutlet UITabBar *MainTabBar;

@end


MainTabViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    UITabBarItem *tabBarItem = [[MainTabBar items] objectAtIndex:1];
    [tabBarItem setEnabled:FALSE];


}

LoginViewController.h



#import <UIKit/UIKit.h>

@interface LoginViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *CustomerUsername;
@property (retain, nonatomic) IBOutlet UITextField *CustomerPassword;
- (IBAction)ResignKeyboardClicked:(id)sender;

@end

LoginViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:@"Login"     style:UIBarButtonItemStyleBordered target:self action:@selector(loginAction)];
    self.navigationItem.rightBarButtonItem = btnGo;

}

- (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];

    return;
    }
    else
    {
        // invalid information
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Invalide Information" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];

        return;

    }
}

For now my code only disable the other 4 tab bar items but I do not know the way to enable all the tab bat items when the user is logged in.

Please help?

Thanks! :D

Answer

RonzyFonzy picture RonzyFonzy · Nov 28, 2012

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