【问题标题】:Enable and disable the tab bar item by a button click in xcode?通过在xcode中单击按钮启用和禁用标签栏项目?
【发布时间】:2012-09-14 17:34:41
【问题描述】:

我有 5 个标签栏项目。第一个将是登录页面。当用户未登录时,其他选项卡项将被禁用,但当用户通过单击导航项按钮登录时,将启用所有其他 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

【问题讨论】:

    标签: xcode uiviewcontroller uitabbarcontroller uitabbar tabbar


    【解决方案1】:

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

    在你的 Storyboard 中创建一个 TabBarController 和所有其他 UIViewController。将它们链接到 TabBarController 并向它们添加分配类。在您的情况下,其中一个 UIViewController 将被称为 LoginViewController。现在,当您的应用启动时,LoginViewController 必须是第一个选项卡,您只需添加以下代码即可禁用选项卡:

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

    所以你的 LoginAction 函数看起来像这样:

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

    【讨论】:

    • 我从没见过有人给出像这样好的答案!对不起,我只能投一票!!
    • 感谢@GabrielMolter!我脸红了一会儿;)
    • 标签栏图标是否会根据其启用状态更改外观?如果是,请附上屏幕截图!
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 2013-06-26
      • 2019-05-11
      相关资源
      最近更新 更多