1. 程式人生 > >iOS開發UITabBarController使用,新增子控制器方法

iOS開發UITabBarController使用,新增子控制器方法

建立一個UITabBarController,匯入需要新增的子控制器進行新增,在AppDelegate裡設定為根檢視

@implementation MainTabBarController

- (void)viewDidLoad {

    [super viewDidLoad];    

   //按照顯示順序建立4個子控制器,並新增

    ViewController1 *vc1 = [[ViewController1 alloc] init];

    [self addChildViewController:vc1 title:@"首頁" imageName:@"ic_tab_home_nor" selectedImageName:@"ic_tab_home_sel"];

    

    ViewController2 *vc2 = [[ViewController2 alloc] init];

    [self addChildViewController:vc2 title:@"分類" imageName:@"ic_tab_home_nor" selectedImageName:@"ic_tab_home_sel"];

    

    ViewController3 *vc3 = [[ViewController3 alloc] init];

    [self addChildViewController:vc3 title:@"訊息" imageName:@"ic_tab_home_nor" selectedImageName:@"ic_tab_home_sel"];

 

    ViewController4 *vc4 = [[ViewController4 alloc] init];

    [self addChildViewController:vc4 title:@"我的" imageName:@"ic_tab_home_nor" selectedImageName:@"ic_tab_home_sel"];

}

 

- (void)addChildViewController:(UIViewController *)childController title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName{

    childController.title = title;

    childController.tabBarItem.image = [UIImage imageNamed:imageName];

    childController.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];

   //每個子控制器新增到導航控制器

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childController];

    [self addChildViewController:nav];

}

@end