在 UIKit 中UITabbar 代表了标签栏,而 UITabBarController 对其进行了封装,和UINavigationController类似,UITabBarController也可以用来控制多个页面导航,用户可在多个视图控制器之间移动,并可定制屏幕底部的选项卡栏,构建一个选项卡栏控制器,首先要为每个按钮准备一个单独的页。每一页都应被创建为UIViewController对象。
构建一个控制器数组:
你的应用程序可能有多个不同的视图控制器,来实现不同的功能。如果你在写一个音乐播放器,可能会有一些控制器,如:MusicList、CurrentPlay、Favourite、SingerList、Settings 等。在创建你的标签栏之前,你应该首先创建一个数组,在其中放置你希望在标签栏中显示的视图控制器对象。
- //生成各个视图控制器
- MusicList* musicList = [[[MusicList alloc] init] autorelease];
- CurrentPlay* currentPlay = [[[CurrentPlay alloc] init] autorelease];
- Favourite* favourite = [[[Favourite alloc] init] autorelease];
- SingerList* singerList = [[[SingerList alloc] init] autorelease];
- Settings* settings = [[[Settings alloc] initWithStyle:UITableViewStyleGrouped] autorelease];
- //加入一个数组
- NSArray* controllerArray = [[NSArray alloc]initWithObjects:musicList,currentPlay,favourite,singerList,settings ,nil];
每个选项卡栏都有它自己的“标签”,定义了他的标签按钮是什么样子。在视图控制器的 init 方法中,可以配置选项卡栏按钮,定义视图的标题与/或 tabBarItem 属性:
- - (id)initWithStyle:(UITableViewStyle)style{
- self = [super initWithStyle:style];
- if (self) {
- self.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"Settings" image:[UIImage imageNamed:@"Setting"] tag:4];
- }
- return self;
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- self.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2];
- }
- return self;
- }
- typedef enum {
- UITabBarSystemItemMore,
- UITabBarSystemItemFavorites,
- UITabBarSystemItemFeatured,
- UITabBarSystemItemTopRated,
- UITabBarSystemItemRecents,
- UITabBarSystemItemContacts,
- UITabBarSystemItemHistory,
- UITabBarSystemItemBookmarks,
- UITabBarSystemItemSearch,
- UITabBarSystemItemDownloads,
- UITabBarSystemItemMostRecent,
- UITabBarSystemItemMostViewed,
- } UITabBarSystemItem;
选项卡栏所需的各个控制器都好了,现在就可以生成我们的选项卡栏控制器了。忘了讲了,控制器我是在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中生成的。
- //创建UITabBarController控制器
- UITabBarController* tabBarController = [[UITabBarController alloc]init];
- //设置UITabBarController控制器的viewControllers属性为我们之前生成的数组controllerArray
- tabBarController.viewControllers = controllerArray;
- // 默认选择第2个视图选项卡(索引从0开始的)
- tabBarController.selectedIndex = 1;
- // 把tabBarController的view作为子视图添加到window
- [self.window addSubview:tabBarController.view];
默认情况下,当按钮多于5个时,选项卡栏控制器允许用户对按钮布局进行定制。要做到这一点,可以单击标有“更多”的标签,然后单击随之出现的导航栏上的编辑按钮。你可以选择只对某些特定的标签进行定制,也可以完全禁止定制。要允许定制,请将选项卡栏控制器的 customizableViewControllers 设置为一个数组,数组中包含有你希望用户进行定制的试图控制器:
导航
当选项卡栏控制器被显示时,控制器自己处理导航操作,会将选中标签对应视图自动切换到屏幕前端。要读取或者更改当前活动的视图控制器,可以使用 selectedViewController 属性:
- tabBarController.selectedViewController = musicList;
- //读取
- UIViewController* activeController = tabBarController.selectedViewController;
- if(activeController == musicList){
- //
- }
- tabBarController.selectedIndex = 1;
要在标签栏上的视图被选中时得到通知,请赋予标签栏控制器一个委托:
- tabBarController.delegate = self;
- - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
- /*添加代码,处理定制标签栏结束之后的操作*/
- }
不断点击一个tab时,右上角的红色数字会不断增加,这个实现方法:
static int Badge_Value = 0;
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (viewController.view.tag == 402)
{
Badge_Value++;
viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",Badge_Value];
}
}
效果如下:
至此结束。