2012-09-22 11 views
12

Tengo 5 elementos de barra de pestañas. La primera será la página de inicio de sesión. Cuando el usuario no haya iniciado sesión, se inhabilitarán otros elementos de bat bat, pero cuando el usuario inicie sesión haciendo clic en el botón navigationItem, se habilitarán los otros 4 elementos de bat tab.¿Habilitar y deshabilitar el elemento de la barra de pestañas con un clic de botón en xcode?

he hecho buscado y encontrado nada ... :(

Aquí está mi código:

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; 

    } 
} 

Por ahora mi código sólo desactivar los otros elementos de la barra 4 pestaña pero no sé el camino para permitir que todos los elementos de murciélago de la ficha cuando el usuario se registra en

por favor, ayudar a

Gracias:.?! D

Respuesta

40

Debo decir que soy un principiante en el desarrollo de iOS, creo que puedo ayudarte.

En tu Storyboard crea un TabBarController y todos los demás UIViewController's. Conéctelos al TabBarController y agregue asignar clases a ellos. En su caso uno de los UIViewController se llamará LoginViewController.Now cuando su aplicación se inicia el LoginViewController debe ser la primera pestaña una que sólo tiene que añadir este código para desactivar las pestañas:

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

Y de nuevo se les puede permitir con:

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

lo tanto, su función LoginAction se vería así:

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

espero que ayudó: D

+0

nunca he visto a nadie dar una respuesta tan bueno como este! ¡Lo siento, solo puedo dar un voto positivo! –

+0

Gracias @GabrielMolter! Me sonrojé por un momento;) – RonzyFonzy

+0

¿Los iconos de la barra de pestañas cambian la apariencia en función de su estado habilitado? Si es así, adjunte un screenie! – fatuhoku

2

He actualizado la solución de @RonzyFonzy trabajar con un número N de elementos barra de pestañas:

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