要自定义UITabBarController,首先我们必须了解UITabBarController结构与其各个相关类的关系(TabBarController、TabBar、TabButton及ViewController)。其中,TabButton是一个双态的Button(选中和未选中),行为和CheckBox、RadioButton类似。TabBar是TabButton的容器,负责TabButton的排布和互斥,保证同时只有一个Button为选中态。TabBarController包含了TabBar,并管理这一个ViewController的栈,在TabBar上的按钮点击时对栈上的ViewController位置进行相应的调整,从而保持TabBar和ViewController栈之间的一致性。
tab bar controller
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColorwhiteColor];
//生成各个视图控制器
a* aa = [[a alloc]init];
b* bb = [[b alloc]init];
c* cc = [[c alloc]init];
d* dd = [[d alloc]init];
e* ee = [[e alloc]init];
//加入一个数组
NSArray* controllerArray = [[NSArray alloc]initWithObjects:aa,bb,cc,dd,ee ,nil];
//创建UITabBarController控制器 (定义在.h中,否在开启arc会自动释放掉,内存访问出错)
tabBarController = [[UITabBarControlleralloc]init];
// 设置委托
tabBarController.delegate = self;
//设置UITabBarController控制器的viewControllers属性为我们之前生成的数组controllerArray
tabBarController.viewControllers = controllerArray;
//默认选择第1个视图选项卡(索引从0开始的)
tabBarController.selectedIndex = 0;
//设置TabBarItem的标题与图片
[(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:0] setTitle:@"aa"];
[(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:0] setImage:[UIImageimageNamed:@"1517.png"]]; //30 * 30
[(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:1] setTitle:@"bb"];
[(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:1] setImage:[UIImageimageNamed:@"light.png"]];
[(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:2] setTitle:@"cc"];
[(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:2] setImage:[UIImageimageNamed:@"podcaste.png"]];
[(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:3] setTitle:@"dd"];
[(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:3] setImage:[UIImageimageNamed:@"setting.png"]];
[(UITabBarItem *)[tabbarController.tabBar.itemsobjectAtIndex:4] setTitle:@"ee"];
[(UITabBarItem *)[tabbarController., tabBar.itemsobjectAtIndex:4] setImage:[UIImageimageNamed:@"unlock.png"]];
//读取
UIViewController* activeController = tabBarController.selectedViewController;
if(activeController == aa){
//
}
// 把tabBarController的view作为子视图添加到window
[self.window addSubview:tabBarController.view];
//定制按钮
/*NSMutableArray* customzableViewControllers = [[NSMutableArray alloc]init];
[customzableViewControllers addObject:musicList];
[customzableViewControllers addObject:currentPlay];
[customzableViewControllers addObject:favourite];
tabBarController.customizableViewControllers = customzableViewControllers;
tabBarController.customizableViewControllers = nil;
*/
//
[self.windowmakeKeyAndVisible];
returnYES;
}
标签页内的项目切换 :
tabBarController.selectedIndex = 1;//(从0开始)
即跳转到第二个子标签页面。