IOS UITabBarController(控制器)的子控制器

UITabBarController的简单使用 

UITabBarController的使用步骤
➢ 初始化UITabBarController
➢ 设置UIWindow的rootViewController为UITabBarController
➢ 根据具体情况,通过addChildViewController方法添加对应个数的子控制器 

 

UITabBarController的子控制器 

UITabBarController添加控制器的方式有2种

➢ 添加单个子控制器

- (void)addChildViewController:(UIViewController *)childController;

➢ 设置子控制器数组
@property(nonatomic,copy) NSArray *viewControllers; 

 

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 1.创建window
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    
    // 设置window的根控制器
    // 1.创建tabbarcontroller
    UITabBarController *tbVc = [[UITabBarController  alloc] init];
// 2.设置UITabBarController为winow的根控制器
    self.window.rootViewController = tbVc;
    
 // 3.创建并添加子控制器
    UIViewController *vc1 = [[UIViewController alloc] init];
    vc1.tabBarItem.title = @"消息";
    vc1.tabBarItem.image = [UIImage imageNamed:@"tab_recent_nor"];
    vc1.tabBarItem.badgeValue = @"998";
//    vc1.tabBarItem.selectedImage
    vc1.view.backgroundColor = [UIColor redColor];
    
    UIViewController *vc2 = [[UIViewController alloc] init];
    vc2.tabBarItem.title = @"联系人";
    vc2.tabBarItem.image = [UIImage imageNamed:@"tab_buddy_nor"];
    vc2.view.backgroundColor = [UIColor greenColor];
    
    UIViewController *vc3 = [[UIViewController alloc] init];
    vc3.view.backgroundColor = [UIColor purpleColor];
    
    [tbVc addChildViewController:vc1];
    [tbVc addChildViewController:vc2];
    [tbVc addChildViewController:vc3];
    
//    tbVc.viewControllers = @[vc1, vc2, vc3];
    
    // 让window显示出来
    [self.window makeKeyAndVisible];
    
    return YES;
}

 

UITabBarButton 

UITabBarButton里面显示什么内容,由对应子控制器的tabBarItem属性决定

UITabBarItem有以下属性影响着UITabBarButton的内容

➢ 标题文字
@property(nonatomic,copy) NSString *title;

➢ 图标
@property(nonatomic,retain) UIImage *image;

➢ 选中时的图标
@property(nonatomic,retain) UIImage *selectedImage;

➢ 提醒数字

@property(nonatomic,copy) NSString *badgeValue; 

 

posted on 2017-03-10 13:43  守望星空  阅读(2231)  评论(0编辑  收藏  举报

导航