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];
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实例。
本文节选自《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》一书。
《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》一书已由电子工业出版社正式出版,本书由虞斌著。
互动出版网:http://product.china-pub.com/198191
分享到:
相关推荐
iphone中的基础控件UITabBar及UITabBarController,适合初学者,主要学习UITabBarController的常用属性设置及其点击事件。 1、UITabBarController是一个容器,一般作为整个程序的rootViewController,容器包含多个...
UITabBarController和UINavigationController的整合使用DEMO,详情见:http://blog.csdn.net/hwe_xc/article/details/50588500
iOS开发UITabBarController和UICollectionView的使用Demo
简单实现UItabbarcontroller标签页面切换
详细说明了UITabBarViewController的用法,包括使用自定义UITabBar以及使用UITabBarController控件
一个小demo,演示如何隐藏tabbar
iOS 自定义UINavigationController和UITabBarController
很多时候我们创建一个基于UITabBarController的application以后还希望能够在每个tab view都可以实现导航控制,即添加一个UINavigationController来实现tabview内部的view之间的切换,这即是本文所要介绍的。
自定义UITabBarController
完美自定义UITabBarController 可实现完全自定义
我们写iOS项目的时候,基本都是一个UINavigationController套一个UITabBarController的形式,就是上面一个导航栏,下面几个按钮的工具条的形式。我写了几个应用,发现如果每次都重新写的话完全就是浪费精力和时间,...
在系统 UITabBarController 的基础上实现安卓版微信 TabBar 的滑动切换功能
实现UINavigationController和UITabBarController结合的一个小例子
适用于初学者的文档,可以很好的了解到UITabBarController的使用方法
页面跳转 UITabBarController+UINavigationController+UIViewController
自定义的UITabBarController,有一定的封装,调用简单方便
UITabBarController Demo IOS 代码 例子
主要介绍了iOS开发中UITabBarController的使用示例,代码基于Objective-C进行演示,需要的朋友可以参考下