2011-12-27 15 views
0

したがって、UIViewControllerにはUITabBarというビューがあります。 JSONをプルダウンして、どのタブをタブバーに追加するかを決めたので、実行時にタブを選択できるようにする必要があります。私はUITabBarController setViewControllers:animated:メソッドを使用して、ビューコントローラのリストを追加したかったのです。しかし、私はビューコントローラでこれをやっているので、タブバーのView Controllerにアクセスする方法はわかりません。ここに私のコードがある...UIViewControllerのUITabBarにUITabBarItemを追加する

ヘッダー

#import <UIKit/UIKit.h> 

@interface HealthTicketTabController : UIViewController <UITabBarDelegate, UITabBarControllerDelegate> { 
    NSArray *viewControllers; 
    IBOutlet UITabBar *tabBar; 
    UIViewController *selectedViewController; 

    // How do I link this the the tabBar's view controller??? 
    UITabBarController *tabBarController; 
} 

@property (nonatomic, retain) NSArray *viewControllers; 
@property (nonatomic, retain) IBOutlet UITabBar *tabBar; 
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController; 
@property (nonatomic, retain) UIViewController *selectedViewController; 

@end 

ソース

- (id)init 
{ 
    self = [super initWithNibName:@"HealthTicketTabView" bundle:nil]; 
    if (self) 
    { 
     //Controllers for the tab view 
     HealthCardController *card = [[HealthCardController alloc] init]; 
     MedicalExpensesController *medical = [[MedicalExpensesController alloc] init]; 

     //Tab bar items to be displayed in the tab bar 
     card.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:0]; 
     medical.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:1]; 

     NSArray *array = [[NSArray alloc] initWithObjects:card, medical, nil]; 

     //Set the tab bar's view controllers to the list 
     tabBarController.viewControllers = [NSArray arrayWithObjects:card, medical, nil]; 

     [array release]; 
     [card release]; 
     [medical release]; 
    } 

    return self; 
} 

答えて

1

は以来、私は現在のUIViewControllerにタブバーのデリゲートを設定し、現在のコントローラのタブのイベントを処理する必要がUITabBarを制御するためにUIViewControllerを使用していたことが分かりました。

ViewControllers切り替えTabBarの

[self.tabBar setItems: tabBarItems animated:TRUE]; 

にTabBarItems追加

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 
{ 
    UIViewController *temp = [viewControllers objectAtIndex:item.tag]; 
    [self.selectedViewController.view removeFromSuperview]; 
    [self.view addSubview:temp.view]; 
    self.selectedViewController = temp; 
} 
1

のUIViewControllerはすでにtabBarController性質を持っています。ヘッダーファイルには1つも必要ありません。これはUITabBarControllerにアクセスする方法です。

関連する問題