UITabBarController 代码实现和框架解析

来源:互联网 发布:mac怎么取消隐藏数据 编辑:程序博客网 时间:2024/06/05 14:33
首先说代码实现 

说明 创建一个单独的类 这个类可以有ib ,也可以没有ib,看你利用那种方式去处理
第一定义个存档控制器的数组

NSMutableArray *controllers =[[NSMutableArray alloc] init];
NSMutableArray *titleControllers =[[NSMutableArray alloc] init];
HomeViewController *_homeViewController = [[HomeViewControlleralloc] init];
ScheduleViewController *_scheduleViewController=[[ScheduleViewController alloc] init];
controllers=[NSArrayarrayWithObjects:_homeViewController,_scheduleViewController,nil];
titleControllers=[NSArrayarrayWithObjects:@"DownloadsURL",@"Downloads", nil];

第二, 去初始化每一个下层控制器 并添加到数组中 举例

UITabBarController <wbr>代码实现和框架解析

这里有两个标签,并且每一标签下都是一个导航器控制,代码如下

UItabBarController*_tabBarController = [[UItabBarController alloc]init];
_tabBarController.viewControllers =controllers;
//由于要显示一个登录界面,这里要把tabBar隐藏了。
_tabBarController.tabBar.hidden =YES;

self.tabBarController =_tabBarController;

//这里是让程序显示的view不是第一个标签的view----(可以是一个登录界面或者其他)//

[self.tabBarController.viewaddSubview:firstViewController.view];

//下面的循环是为每一个标签设置title和图片
for (UIViewController*_viewController in _tabBarController.viewControllers)
{
_viewController.tabBarItem.title =[titleArray objectAtIndex:
[_tabBarController.viewControllersindexOfObject:_viewController]];
_viewController.tabBarItem.image =[UIImage imageNamed:[imageArrayobjectAtIndex:[_tabBarController.viewControllersindexOfObject:_viewController]]];

}
_tabBarController.delegate = self;
[windowaddSubview:_tabBarController.view];  





原创粉丝点击