- (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);