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

猜你喜欢

转载自blog.csdn.net/ljw2017/article/details/84325013