代码之家  ›  专栏  ›  技术社区  ›  user1580957

通过在xcode中单击按钮启用和禁用选项卡栏项目?

  •  14
  • user1580957  · 技术社区  · 12 年前

    我有5个选项卡栏项目。第一个将是登录页面。当用户尚未登录时,其他选项卡项将被禁用,但当用户通过单击navigationItem按钮登录时,所有其他4个选项卡项都将被启用。

    我进行了搜索,但一无所获…:(

    这是我的代码:

    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;
    
        }
    }
    

    目前,我的代码只禁用了其他4个选项卡栏项目,但我不知道在用户登录时如何启用所有选项卡栏项目。

    请帮忙?

    谢谢!:D

    2 回复  |  直到 12 年前
        1
  •  40
  •   Cœur Gustavo Armenta    7 年前

    我不得不说,我是iOS开发的初学者,我想我可以帮助你。

    在情节提要中制作一个TabBarController和所有其他UIViewController。将它们链接到TabBarController,并为它们添加赋值类。在您的情况下,其中一个UIViewController将被称为LoginViewController。现在,当您的应用程序启动时,LoginViewControl必须是第一个选项卡,您只需添加以下代码即可禁用选项卡:

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

    同样,您可以通过以下方式启用它们:

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

    因此,您的登录操作功能将如下所示:

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

    我希望它能有所帮助:D

        2
  •  3
  •   ChavirA    10 年前

    我从@RonzyFonzy更新了解决方案,以使用N个选项卡栏项目:

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