一、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,隐藏多出来的视图控制器)

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

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);

}

转载于:https://www.cnblogs.com/zfrankice/p/4238998.html

UITabBarController 基本用法相关推荐

  1. UITabBarController 笔记(三) UITabBarController 配合 UINavigationController 的使用

    建个空的iOS工程 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictiona ...

  2. UIDeviceOrientation 和 UIInterfaceOrientation 设备旋转的用法 (实例)  和 IOS6屏幕旋转详解

    1. UIDeviceOrientation 和 UIInterfaceOrientation 设备旋转的用法 (实例) 博客分类: IOS / Objective-C UIDeviceOrienta ...

  3. c语言中external,static关键字用法

    static用法: 在C中,static主要定义全局静态变量.定义局部静态变量.定义静态函数. 1.定义全局静态变量:在全局变量前面加上关键字static,该全局变量变成了全局静态变量.全局静态变量有 ...

  4. Pandas_transform的用法

    先来看一个实例问题. 如下销售数据中展现了三笔订单,每笔订单买了多种商品,求每种商品销售额占该笔订单总金额的比例.例如第一条数据的最终结果为:235.83 / (235.83+232.32+107.9 ...

  5. Python中yield和yield from的用法

    yield 后面接的是 future 对象 调用方 委托生成器 yield from 直接给出循环后的结果 yield from 委托者和子生成器直接通信 yield from 直接处理stopIte ...

  6. pytorch学习 中 torch.squeeze() 和torch.unsqueeze()的用法

    squeeze的用法主要就是对数据的维度进行压缩或者解压. 先看torch.squeeze() 这个函数主要对数据的维度进行压缩,去掉维数为1的的维度,比如是一行或者一列这种,一个一行三列(1,3)的 ...

  7. python yield 和 yield from用法总结

    #例1. 简单输出斐波那契數列前 N 个数 #缺点:该函数可复用性较差,因为 fab 函数返回 None,其他函数无法获得该函数生成的数列 #要提高 fab 函数的可复用性,最好不要直接打印出数列,而 ...

  8. tf.nn.embedding_lookup()的用法

    函数: tf.nn.embedding_lookup( params, ids, partition_strategy='mod', name=None, validate_indices=True, ...

  9. OpenMP用法大全

    OpenMP基本概念 OpenMP是一种用于共享内存并行系统的多线程程序设计方案,支持的编程语言包括C.C++和Fortran.OpenMP提供了对并行算法的高层抽象描述,特别适合在多核CPU机器上的 ...

最新文章

  1. “学了半年后,我要揭开Python 3宗罪!”
  2. 硅谷顶级VC:“S曲线”看四大风口,创企成功机会巨大
  3. JVM 参数及各部分含义(转)
  4. 单片机 c语言 按键长按短按,求助:单片机一键长按与短按按键实现的c程序有问题...
  5. c++创建文件_JavaNote 文件系统及Java文件基本操作
  6. 阻止button刷新页面
  7. java 面试心得总结-BAT、网易
  8. some screenshot for SAP Fiori smart template resource load
  9. MySQL的命令合集
  10. Java中使用JNI调用本地动态库的方法
  11. pb 选中树形菜单节点_动态绑定树形菜单,并搜索节点展开
  12. 小tips:善用页面的base类
  13. [java] 简单的ConcurrentHashMap
  14. JSP程序设计课后习题答案
  15. c语言读取三菱plc数据,使用用三菱小软件读取三菱PLC数据的方法
  16. html中有序列表的css样式,CSS 列表样式(ul)
  17. cass小插件集合_CAD面积插件大全_CAD插件大全_CASS插件大全_小懒人CAD插件老妈砂锅串串香加盟...
  18. 临平职高计算机分数线,权威发布!余杭区2017年各类高中招生第一批次录取分数线划定!...
  19. 分集增益的不同合并方式的性能分析
  20. SQL AlawaysOn 之三:SQL服务器加入域

热门文章

  1. centos nginx重启_nginx学习笔记
  2. python做圆柱绕流_圆柱绕流
  3. mybatis多个foreach_使用 Mybatis 的 foreach 批量模糊 like 查询及批量插入
  4. envi中的sg滤波_ENVI滤波
  5. python 字符串去重且相同字符最多出现2次_Python实现计算字符串中出现次数最多的字符示例...
  6. java ftp分片续传_Java写的支持断点续传的FTP
  7. 高仿国美在线底部代码实现
  8. python gui测试框架_八款常用的 Python GUI 开发框架推荐
  9. Vue学习(入门实例、常用指令)-学习笔记
  10. 18-switch语句