您的位置:首页 > 产品设计 > UI/UE

iPhone SDK开发基础之使用UITabBarController组织和管理UIView

2011-06-02 14:58 513 查看
iPhone SDK开发基础之
使用UITabBarController组织和管理UIView

当你的程序分为几个相对比较独立的部分时,就比较适合使用UITabBarController来组织用户界面,如图3-26所示。



在屏幕的下方包含UITabBarController的三个按钮,用户单击不同的按钮即可以进入不同的界面,每个界面相对来说在整个系统中比较独立,也就是程序分成了三个相对比较独立的不同部分,在每个相对独立的部分你也可以使用UINavigationController等容器类组织你的界面。这样组织使程序逻辑非常清晰,当然你也可以组织很多个Tab而不只是三个,以下代码演示如何创建UITabBarController对象,并为其添加多个Tab。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {

// 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];
mailto:secondViewController.navigationItem.title=@%22Q & 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];
mailto:thirdViewController.navigationItem.title=@%22Misc";

[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实例。

本文节选自《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》一书。
《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》一书已由电子工业出版社正式出版,本书由虞斌著




互动出版网:http://product.china-pub.com/198191
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐