// Override point for customization after application launch.
//Create the navigation Controller
UINavigationController *localNavigationController;
//Create UINavigationController
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
// Create the array that will contain all the View controlelr
NSMutableArray *localControllersArray = [[NSMutableArray alloc] init WithCapacity:3];
// Create the view controller attached to the first item in the TabBar
aViewController *firstViewController;
firstViewController = [aViewController alloc];
localNavigationController = [[UINavigationController alloc] initWithRoot ViewController:firstViewController];
localNavigationController.navigationBar.barStyle. = UIBarStyleBlackOpaque;
[localNavigationController.tabBarItem initWithTitle:@"Outlines"
image:[UIImage imageNamed:@"webcast.png"] tag:1];
firstViewController.navigationItem.title = @"Outlines";
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[firstViewController release];
// Create the view controller attached to the second item in the TabBar
anotherViewController *secondViewController;
secondViewController = [[anotherViewController alloc] initWithStyle. UITableViewStyleGrouped ];
localNavigationController = [[UINavigationController alloc] initWithRoot ViewController:secondViewController];
[localNavigationController.tabBarItem initWithTitle:@"Q & A"
image:[UIImage imageNamed:@"book.png"] tag:2];
secondViewController.navigationItem.title=@"Q & A";
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[secondViewController release];
miscViewController *thirdViewController;
thirdViewController = [[miscViewController alloc] initWithStyle.:UITable ViewStyleGrouped ];
localNavigationController = [[UINavigationController alloc] initWithRoot ViewController:thirdViewController];
[localNavigationController.tabBarItem initWithTitle:@"Misc"
image:[UIImage imageNamed:@"favorites.png"] tag:3];
thirdViewController.navigationItem.title=@"Misc";
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[thirdViewController release];
// load up our tab bar controller with the view controllers
tabBarController.viewControllers = localControllersArray;
// release the array because the tab bar controller now has it
[localControllersArray release];
// add the tabBarController as a subview in the window
[window addSubview:tabBarController.view];
// need this last line to display the window (and tab bar controller)
[window makeKeyAndVisible];
return YES;
}
捕获Tab切换事件,获取当前活动的Tab索引和UIViewController对象,代码如下。
- (void)tabBarController:(UITabBarController *)barController didSelectView Controller:(UIViewController *)viewController{
NSLog(@"currentController index:%d",viewController, tabBarController.selectedIndex);
UIViewController *currentController = tabBarController.selectedView Controller;
NSLog(@"currentController: %@",currentController);
}
切换不同的Tab时,只需要设置UITabBarController的selectedIndex属性即可,代码如下。
tabBarController.selectedIndex = 2;
本节相关的完整Xcode工程源代码文件请参考本书附带的光盘中的Lessons2实例。