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
使用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
相关文章推荐
- iPhone SDK开发基础之使用UINavigationController组织和管理UIView
- iPhone SDK开发基础之使用UITabBarController组织和管理UIView
- iPhone SDK开发基础之使用UINavigationController组织和管理UIView
- 在solr的源码的基础上进行开发,把solr 4.4.0的源码导入到本地eclipse中,使用maven进行管理。
- 使用Metronic开发的后台管理系统的基础架构(bootstrap 3.3.5)
- iOS开发基础-UIView的使用
- iOS开发UI基础—08应用程序管理之xib的简单使用
- IOS开发基础—通过联系人管理实例学习xib的基本使用
- ios开发-UI基础-应用管理(单纯界面)改进5-使用代理实现监听下载按钮的点击(delegate)
- iOS开发UI基础—31在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建
- ios开发-UI基础-应用管理(单纯界面)改进3-xib使用
- 使用Metronic开发的后台管理系统基础架构
- GitHub基础使用(版本管理,个人博客)
- 一步一步教你使用AgileEAS.NET基础类库进行应用开发-基础篇-基于接口驱动的数据层
- 玩转Android Camera开发(二):使用TextureView和SurfaceTexture预览Camera 基础拍照demo
- 使用Java进行FreeMarker的web模板开发的基础教程
- 【Android开发基础】应用界面主题Theme使用方法
- 去年5月份,公司项目较少,我就组织了一个用于公司内部办公管理的项目(以下称OA)。第一个目的是为了让公司里面一些技术较差的人员得到真正编程的锻炼;第二个目的是为了使用空闲的人员为公司开发一个产品,在内
- IOS开发基础—uiscrollview的基本使用
- 一步一步使用Ext JS MVC与Asp.Net MVC 3开发简单的CMS后台管理系统之完成登录功能