UITabBarController的用法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
FirstViewController *firstController=[[FirstViewController alloc] init];
//设置标题
firstController.title=@"First";
//设置tabBarItem的样式(这种方式是按照系统定义的方式)
firstController.tabBarItem=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMoretag:101];
//也可以用自定义方式方式如下:
/*
firstController.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"First" image:[UIImage imageNamed:@"img.png"] tag:101];*/
SecondViewController *secondController=[[SecondViewController alloc] init];
secondController.title=@"Second";
secondController.tabBarItem=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistorytag:102];
ThirdViewController *thirdController=[[ThirdViewController alloc] init];
thirdController.title=@"Third";
thirdController.tabBarItem=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavoritestag:103];
UINavigationController *nav1=[[UINavigationController alloc] initWithRootViewController:firstController];
[firstController release];
UINavigationController *nav2=[[UINavigationController alloc] initWithRootViewController:secondController];
[secondController release];
UINavigationController *nav3=[[UINavigationController alloc] initWithRootViewController:thirdController];
[thirdController release];
NSArray *controllersArray=[[NSArrayalloc] initWithObjects:nav1,nav2,nav3, nil];
[nav1 release];
[nav2 release];
[nav3 release];
UITabBarController *tabController=[[UITabBarController alloc] init];
tabController.viewControllers=controllersArray;
tabController.selectedIndex=0;
tabController.delegate=self;//在AppDelegate.h文件内实现UITabBarControllerDelegate协议
self.tabBarController=tabController;
[tabController release];
[controllersArray release];
[self.window addSubview:tabController.view];
[self.window makeKeyAndVisible];
returnYES;
}
//当点击tabBarItem时触发该操作 UITabBarControllerDelegate中的一个方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *) viewController {
NSLog(@"%d", viewController.tabBarItem.tag);