关于IOS的导航控制器和工具栏 是IOS开发中一定会用到的知识,好在苹果给开发者们封装好了 导航控制器的样式。使开发者运用起来非常方便。
个人觉得IOS的TabBar比android的要简单的多(-----此处废话)
1、定义好n个视图文件
-
2、在APP2AppDelegate.m文件里设置根视图 看代码
#import "DongtaiView.h"
#import "MessageView.h"
#import "XiaoyouView.h"
#import "ThreeYearViewr.h"
#import "ClubView.h"
#import "OtherViewr.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
//UITabBar 选项卡
//UITabBarItem 每一个选项
//通过UITabBar来显示 UITabBarItem的实例
//实例化视图控制器
DongtaiView *dongtaiVC = [[DongtaiViewr alloc] init];
//定义每一个选项 使用系统图标
//UITabBarItem *dtItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1];
UINavigationController *navDongtai = [[UINavigationController alloc] initWithRootViewController:dongtaiVC];
//自定义图片
UITabBarItem *dtItem = [[UITabBarItem alloc] initWithTitle:@"动态" image:[UIImage imageNamed:@"0157"] tag:1];
navDongtai.tabBarItem = dtItem;
[dtItem release];
//显示消息选项
MessageView *msgVc = [[MessageView alloc] init];
UINavigationController *navMsg = [[UINavigationController alloc] initWithRootViewController:msgVc];
//定义每一个选项
UITabBarItem *msgItem = [[UITabBarItem alloc]initWithTitle:@"消息" image:[UIImage imageNamed:@"0013"] tag:2];
navMsg.tabBarItem = msgItem;
[msgItem release];
//显示校友选项
XiaoyouView *xiaoyouVc = [[XiaoyouViewr alloc] init];
UINavigationController *navXiaoyou = [[UINavigationController alloc] initWithRootViewController:xiaoyouVc];
//定义每一个选项
UITabBarItem *xiaoyouItem = [[UITabBarItem alloc]initWithTitle:@"校友" image:[UIImage imageNamed:@"0011"] tag:3];
navXiaoyou.tabBarItem = xiaoyouItem;
[xiaoyouItem release];
//显示CSDN三年选项
ThreeYearViewr *threeyearVc = [[ThreeYearView alloc] init];
UINavigationController *navThreeYear = [[UINavigationController alloc] initWithRootViewController:threeyearVc];
//定义每一个选项
UITabBarItem *threeyearItem = [[UITabBarItem alloc]initWithTitle:@"CSDN三年" image:[UIImage imageNamed:@"0151"] tag:4];
navThreeYear.tabBarItem = threeyearItem;
[threeyearItem release];
//显示俱乐部选项
ClubView *clubVc = [[ClubViewr alloc] init];
UINavigationController *navClub = [[UINavigationController alloc] initWithRootViewController:clubVc];
//定义每一个选项
UITabBarItem *clubItem = [[UITabBarItem alloc]initWithTitle:@"高校俱乐部" image:[UIImage imageNamed:@"0300"] tag:5];
navClub.tabBarItem = clubItem;
[clubItem release];
//显示俱乐部选项
OtherViewr *otherVc = [[OtherView alloc] init];
UINavigationController *navOther = [[UINavigationController alloc] initWithRootViewController:otherVc];
//定义每一个选项
UITabBarItem *otherItem = [[UITabBarItem alloc]initWithTitle:@"其他" image:[UIImage imageNamed:@"0233"] tag:6];
navOther.tabBarItem = otherItem;
[otherItem release];
//定义数组
NSArray *arrVc =@[navDongtai,navMsg,navXiaoyou ,navThreeYear,navClub,navOther];
//定义UITabBarController
UITabBarController *tabBar = [[UITabBarController alloc] init];
tabBar.viewControllers = arrVc;
//注意要把当前的tabBar 设置为根视图,这样才能保我们切换那一个view这个工具栏都存在
self.window.rootViewController = tabBar;
[dongtaiVC release];
[msgVc release];
[navMsg release];
[xiaoyouVc release];
[threeyearVc release];
[clubVc release];
[otherVc release];
[navDongtai release];
[navClub release];
[navOther release];
[navThreeYear release];
[navXiaoyou release];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
3、在各个分视图中定义显示的view 如:
在DongtaiView.m中 重写父类的loadView方法
-(void)loadView{
UIView *baseView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
baseView.backgroundColor = [UIColor redColor];
self.view = baseView;
[baseView release];
}
其他的分栏也是这样。
OK就是这样。别看整个项目的文件和代码不少,但是大多是重复操作。因为苹果已经开发者铺好了路,我们照着走就行了。