UITabBar十分常用,它能实现多个页面的快速切换,而且看起来简单实用。
假设我这有多个已经初始化好的viewController,然后需要将它们加入到TabBarController中,并把其设置为根视图
NSArray *views = [[NSArray alloc] initWithObjects:test, time, test1, test2, test3, test4, nil]; UITabBarController *tbc = [[UITabBarController alloc] init]; tbc.viewControllers = views; self.window.rootViewController = tbc;
这里的每个View的Item主要有这么几个属性,标题title,图片image,以及time右上的圆圈图标,显示的是badgeValue。可以在覆写view的初始化方法的时候设置它的tabBarItem属性。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.tabBarItem.title = @"time"; self.tabBarItem.image = [UIImage imageNamed:@"time.png"]; self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", 3]; } return self; }
点击Edit
另外还可以设置delegate协议,来实现委托方法,相关的协议有UITabBarDelegate和UITabBarControllerDelegate
UITabBarDelegate
它主要有5个方法,其中最后一个是required,这个方法是在用户选择一个标签栏时发送委托,需要实现功能。
UITabBarControllerDelegate
得益于OBJ-C冗长的方法名,方法都不是很难理解。看看API大概就都能使用了。