UITabBarController 笔记(二) ViewController中加UITabBarController
笔记 中加
2023-09-11 14:17:59 时间
新建一个简单视图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,所以嘛。。。。。不多说了,你懂的!!
相关文章
- 【学习笔记34】JavaScript的时间对象
- NMAP学习笔记
- VS Code配置Matlab环境笔记-文本编辑、语法检查、运行脚本
- 《模式识别》学习笔记(七)聚类算法:动态聚类算法之C均值聚类
- PHP全栈学习笔记32
- PHP 设计模式 笔记与总结(9)数据对象映射模式
- Spring源码学习笔记——Bean加载
- Android学习笔记进阶十之Matrix错切变换
- UITabBarController 笔记(一)AppDelegate中加UITabBarController 为 rootViewController
- CSDN生存笔记之阅读突破80万
- <SPECTRAL-SPATIAL CLUSTERING OF HYPERSPECTRAL IMAGE BASED ON LAPLACIAN REGULARIZED DEEP SUBSPACE >笔记
- 自己学习Cesium的笔记简介
- 外设驱动库开发笔记15:DHT11温湿度传感器驱动
- pyqt5 进度条使用笔记
- 前端自学笔记:JSON、JSONP和JS之间的区别,Ajax、axios和jsonp的区别
- 《慕客网:IOS基础入门之Foundation框架初体验》学习笔记 <五> NSDicionary + NSMutableDictionary
- Linux下汇编语言学习笔记10 ---