2012-09-22 5 views
12

私は5つのタブバー項目を持っています。最初のものはログインページになります。ユーザーが他のタブにログオンしていないときは、バットアイテムは無効になりますが、navigationItemボタンをクリックしてログオンすると、他の4つのタブバットアイテムが有効になります。私が作ったボタンでタブバーの項目を有効または無効にするには、xcodeをクリックしますか?

を検索し、何も見つからなかった... :(

ここに私のコードです:今、私のコードの場合

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

答えて

40

私はiOS開発の初心者だと言わざるを得ません。私はあなたを助けることができると思います。

ストーリーボードでは、TabBarControllerと他のすべてのUIViewControllerを作成します。それらをTabBarControllerにリンクし、それらにassignクラスを追加します。あなたのアプリがLoginViewControllerは、最初のタブでなければなりませんANあなたは、単にタブを無効にするには、このコードを追加開始したときに、あなたのケースでのUIViewControllerの一つがLoginViewController.Now呼び出されます。

[[[[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の機能は、次のようになります。D

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

私はそれが助けを願って

+0

私はこの1つのようなので、良い答えを与える誰も見たことがありません!私は1つupvoteを与えることができます申し訳ありません! –

+0

ありがとう@GabrielMolter!私は一瞬blushed;) – RonzyFonzy

+0

タブバーのアイコンは、有効な状態に基づいて外観が変わるのですか?もしそうなら、スクリーンをつけてください! – fatuhoku

2

私はタブバー項目のN数で動作するように@RonzyFonzyからソリューションを更新:

for (UITabBarItem *tmpTabBarItem in [[self.tabBarController tabBar] items]) 
      [tmpTabBarItem setEnabled:NO]; 
関連する問題