UITabBarController 基本用法

 

 

 

一、UITabBarController对象的创建和初始化

 

首先创建多个视图控制器,并在tabBarItem中添加标题和图片

 

   self.viewController.tabBarItem.title = @"首页";//设置tabBarItem标题

    self.viewController.tabBarItem.image = [UIImage imageNamed:@"icon_home"];//添加图片

    self.viewController.tabBarItem.badgeValue = @"1";//消息

    FirstViewController *one = [[FirstViewController alloc] init];

    one.tabBarItem.title = @"消息";

    one.tabBarItem.image = [UIImage imageNamed:@"icon_meassage"];

    SecondViewController *two = [[SecondViewController alloc] init];

    two.tabBarItem.title = @"联系人";

    two.tabBarItem.image = [UIImage imageNamed:@"icon_selfinfo"];

    ThirdViewController *three = [[ThirdViewController alloc] init];

   three.title = @"搜索";

    three.tabBarItem.image = [UIImage imageNamed:@"icon_search"];

    ForthViewController *four = [[ForthViewController alloc] init];

    four.tabBarItem.title = @"更多";

    four.tabBarItem.image = [UIImage imageNamed:@"icon_more"];

    FifthViewController *five = [[FifthViewController alloc]init];

    five.tabBarItem.title = @"订阅";

    SixthViewController *six = [[SixthViewController alloc] init];

    six.tabBarItem.title = @"下载";

    

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

 

  注意,第三个视图控制器为导航控制器 

 

将多个视图控制器添加到数组(视图超过5个以后,自动生成More,隐藏多出来的视图控制器)

效果为多出来下边一排视图切换工具

UITabBarController <wbr>基本用法

UITabBarController <wbr>基本用法    NSArray *vcArray = [NSArray arrayWithObjects:self.viewController, one, two, nav, four, five, six, nil];//创建视图控制器数组 

    UITabBarController *tabBarC = [[UITabBarController alloc] init];

    tabBarC.tabBar.selectedImageTintColor = [UIColor redColor];//选中时图片颜色变为红色

    tabBarC.viewControllers = vcArray;//将多个视图控制器组织到tab控制器中,以选项卡

 

   self.window.rootViewController = tabBarC;//设置根视图控制器为tabBarController

 

二、UITabBarController可以实现UITabBarControllerDelegate协议

 

tabBarC.delegate = self; //设置self为代理

可以重写代理中方法(很少用),表示选中了某个ViewController

 

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController*)viewController{

//执行相应操作

}

 

 

//代码解说

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    FirstViewController *firstController=[[FirstViewController alloc] init];

    //设置标题

    firstController.title=@"First";

    //设置tabBarItem的样式(这种方式是按照系统定义的方式)

    firstController.tabBarItem=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMoretag:101];

    //也可以用自定义方式方式如下:

    

    

    SecondViewController *secondController=[[SecondViewController alloc] init];

    secondController.title=@"Second";

    secondController.tabBarItem=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistorytag:102];

    ThirdViewController *thirdController=[[ThirdViewController alloc] init];

    thirdController.title=@"Third";

    thirdController.tabBarItem=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavoritestag:103];

    

    UINavigationController *nav1=[[UINavigationController alloc] initWithRootViewController:firstController];

    [firstController release];

    UINavigationController *nav2=[[UINavigationController alloc] initWithRootViewController:secondController];

    [secondController release];

    UINavigationController *nav3=[[UINavigationController alloc] initWithRootViewController:thirdController];

    [thirdController release];

    NSArray *controllersArray=[[NSArrayalloc] initWithObjects:nav1,nav2,nav3, nil];

    [nav1 release];

    [nav2 release];

    [nav3 release];

    

    UITabBarController *tabController=[[UITabBarController alloc] init];

    tabController.viewControllers=controllersArray;

    tabController.selectedIndex=0;

    tabController.delegate=self;//在AppDelegate.h文件内实现UITabBarControllerDelegate协议

    self.tabBarController=tabController;

    [tabController release];

    [controllersArray release];

    

    [self.window addSubview:tabController.view];

 

    [self.window makeKeyAndVisible];

    returnYES;

}

 

//当点击tabBarItem时触发该操作  UITabBarControllerDelegate中的一个方法

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {

    NSLog(@"%d", viewController.tabBarItem.tag);

}

 

posted @ 2015-01-21 15:25  ︶ㄣK__ぺ  阅读(158)  评论(0编辑  收藏  举报