UITabBarController(底部控制栏)
创建
ViewControllerA * ctlA = [ViewControllerA new];
UINavigationController * navA = [[UINavigationController alloc] initWithRootViewController:ctlA];
ViewControllerB * ctlB = [ViewControllerB new];
ViewControllerC * ctlC = [ViewControllerC new];
ViewControllerD * ctlD = [ViewControllerD new];
ViewControllerE * ctlE = [ViewControllerE new];
ViewControllerF * ctlF = [ViewControllerF new];
ctlA.view.backgroundColor = [UIColor redColor];
navA.title = @"红色";
ctlB.view.backgroundColor = [UIColor greenColor];
ctlB.title = @"绿色";
ctlC.view.backgroundColor = [UIColor purpleColor];
ctlC.title = @"紫色";
ctlD.view.backgroundColor = [UIColor yellowColor];
ctlD.title = @"黄色";
ctlE.view.backgroundColor = [UIColor blueColor];
ctlE.title = @"蓝色";
ctlF.view.backgroundColor = [UIColor brownColor];
ctlF.title = @"棕色";
UITabBarController * tabCtl = [UITabBarController new];
tabCtl.viewControllers = @[navA, ctlB, ctlC, ctlD, ctlE, ctlF];
self.window.rootViewController = tabCtl;
如果需要设置按钮的图标,可以通过设置 tabBarItem 来实现:
navA.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:0];
navA.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld", 10L];
还可以:
navA.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"红色" image:image selectedImage:imageSelected];
当然,创建的位置也可以在 ViewController 中重载的 initWithNibName:
- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self != nil) {
// UIImage * image = [UIImage imageNamed:@"1_normal.png"];
// UIImage * imageSelected = [UIImage imageNamed:@"1_selected.png"];
//
// self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"红色" image:image selectedImage:imageSelected];
//
// self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:0];
//
// self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld", 10L];
}
return self;
}
保存用户喜好
技术支持
通过 NSUserDefaults 中的单例方法来获取当前应用程序的配置对象
NSUserDefaults * def = [NSUserDefaults standardUserDefaults];
保存用户最后选中的标签编号
NSUserDefaults * def = [NSUserDefaults standardUserDefaults];
NSInteger index = tabBarController.selectedIndex;
[def setInteger:index forKey:@"index"];
[def synchronize];
应用启动时,加载用户最后选中的标签编号
NSInteger index = [[NSUserDefaults standardUserDefaults] integerForKey:@"index"];
tabCtl.selectedIndex = index;
处理TabBar的事件
需要将TabBar的事件委托给遵循了 UITabBarControllerDelegate 协议的对象
// 当前项是否可以选择? 可以返回YES,否则返回NO
shouldSelectViewController
// 用户已经选择了
didSelectViewController
通过以上两个接口,可以通过接口中的参数 tabBarController.selectedIndex 实现获取用户最终选择 index 项的功能.
【注意】在6个及6个以上的按钮时,会出现“More” 的按钮,才能调用如下三种事件处理消息。
// 将开始配置按钮的顺序
willBeginCustomizingViewControllers
// 将要结束配置按钮的顺序
willEndCustomizingViewControllers
// 已经完成按钮的顺序配置
didEndCustomizingViewControllers
通过最后完成的事件处理,已经接口参数中的 viewControllers 来实现保存用户最后按钮的排列顺序
保存及加载按钮的排列顺序
【注意】保存的按钮需要保存,ctrl中的字符串,因为如果保存 ctrl 对象时,再加载时,对象已经消失了,重新恢复已经消失对象地址,已毫无意义!
- (void) tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
if (changed) {
NSLog(@"changed");
NSMutableArray * mArr = [NSMutableArray new];
// 保存的时候,不能保存对象地址,因为再加载时,对象已经销毁了。
for (UIViewController * ctl in viewControllers) {
[mArr addObject:ctl.title];
}
NSUserDefaults * def = [NSUserDefaults standardUserDefaults];
[def setObject:mArr forKey:@"array"];
[def synchronize];
}
NSLog(@"didEndCustomizingViewControllers");
}
UITabBarController * tabCtl = [UITabBarController new];
tabCtl.viewControllers = @[navA, ctlB, ctlC, ctlD, ctlE, ctlF];
NSArray * arr = [[NSUserDefaults standardUserDefaults] valueForKey:@"array"];
if (arr) {
NSMutableArray * mNewArr = [NSMutableArray new];
for (NSString * title in arr) {
for (UIViewController * ctl in tabCtl.viewControllers) {
if ([title isEqualToString:ctl.title]) {
[mNewArr addObject:ctl];
break;
}
}
}
tabCtl.viewControllers = mNewArr;
}