新建一个简单视图iOS工程,在ViewController的viewDidLoad中代码如下

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tabBarCtrl = [[UITabBarController alloc] init];//要加入的TabBarController
tabBarCtrl.delegate = self;
//
firstViewCtl = [[UIViewController alloc] init];
firstViewCtl.title = @"first view controller";
firstViewCtl.view.backgroundColor = [UIColor blueColor];
firstViewCtl.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"rename first" image: nil tag:1];//自定义tabBarItem,可以贴tabBarItem背景图片,如果不写,tabBarItem的标签将显示为firstViewCtl.title内容
secondViewCtrl = [[UIViewController alloc] init]; secondViewCtrl.title = @"second view controller"; secondViewCtrl.view.backgroundColor = [UIColor redColor]; 
secondViewCtrl.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",8]; //tabBarItem红色小圈中显示数字
secondViewCtrl.tabBarItem.tag = 2; //加标示
NSArray *arryViewContrller = [NSArray arrayWithObjects:firstViewCtl, secondViewCtrl,nil]; tabBarCtrl.viewControllers = arryViewContrller; tabBarCtrl.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); [self.view addSubview:tabBarCtrl.view]; }

添加委托,ViewController.h中

 
@interface ViewController : UIViewController<UITabBarDelegate>
{
UITabBarController *tabBarCtrl;
UIViewController *firstViewCtl;
UIViewController *secondViewCtrl;
}
 

tabBarItem点击响应

 
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{ NSString *strTitle = viewController.title;
NSLog(@"Title:%@ tag:%d", strTitle , viewController.tabBarItem.tag);

if(1 == viewController.tabBarItem.tag)

   {

viewController.tabBarItem.badgeValue = [NSStringstringWithFormat:@"%d", rand()%100];

}

}
 

小结:

UITabBarController 继承自UIViewController,所以嘛。。。。。不多说了,你懂的!!

最新文章

  1. web初学之MVC
  2. ArcGIS中的三种查询
  3. Codeforces Testing Round #10 A. Forgotten Episode
  4. Entity Framework &amp;&amp; Lambda
  5. CSS3--选择器
  6. 写给自己看的Linux运维基础(四) - python环境
  7. 区别CALL SCREEN/SET SCREEN/LEAVE TO SCREEN
  8. java_接口和抽象类的区别
  9. 运行edX Devstack
  10. 找出整数中第k大的数
  11. Windows Minifilter驱动 - 调式 (4)
  12. MVCC的一种实现方案
  13. 【剑指offer】复制的复杂链条
  14. 支持多个版本的ASP.NET Core Web API
  15. Linux(CentOS6.5)下创建新用户和组,并制定用户和组ID
  16. mysql函数调用过程
  17. 基于SSM的CRUD项目的详解
  18. 洛谷P1776--宝物筛选(单调队列+多重背包)
  19. 彻底理解mysql服务器的字符集转换问题
  20. jQuery调用Asp.Net后台方法

热门文章

  1. 组队练习赛(Regionals 2012, North America - East Central NA)
  2. 【PAT】1025. PAT Ranking (25)
  3. thinkphp中的where()方法
  4. C#中ref和out的使用小结
  5. Windows去掉桌面SVN文件或文件夹问号
  6. OSCHina技术导向:Java轻量web开发框架——JFinal
  7. [x-means] 1.x-means简介
  8. 在 Mac OS X Lion 下修改 Hosts 的四种方法
  9. mobilebone.js 移动web APP单页切换骨架
  10. win7充分利用cpu来提供计算机性能