ios开发--UITabBarController使用详解

来源:互联网 发布:淘宝快递多少钱 编辑:程序博客网 时间:2024/06/06 17:11
ios开发--UITabBarController使用详解

  首先我们看一下它的view层级图:


UITabBarController是IOS中很常用的一个viewController,例如系统的闹钟程 序,ipod程序等。UITabBarController通常作为整个程序的rootViewController,而且不能添加到别的 container viewController中。

  首先我们看一下它的view层级图:


一、手动创建UITabBarController

  最常见的创建UITabBarController的地方就是在application delegate中的 applicationDidFinishLaunching:方法,因为UITabBarController通常是作为整个程序的rootViewController的,我们需要在程序的window显示之前就创建好它,具体步骤如下:

  1、创建一个UITabBarController对象

  2、创建tabbarcontroller中每一个tab对应的要显示的对象

  3、通过UITabBarController的viewController属性将要显示的所有content viewcontroller添加到UITabBarController中

  4、通过设置UITabBarController对象为window.rootViewController,然后显示window

  下面看一个简单的例子:

复制代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]  autorelease]; // Override point for customization after application launch.    SvTabBarFirstViewController *viewController1, *viewController2;viewController1 = [[SvTabBarFirstViewController alloc] initWithNibName:nil bundle:nil]; viewController1.title @"First"; viewController2 =[[SvTabBarFirstViewController alloc] initWithNibName:nil bundle:nil]; viewController2.title @"Second"; self.tabBarController = [[[UITabBarController alloc] init] autorelease]; self.tabBarController.delegate = self; self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil]; 
[viewController1 release]; [viewController2 release]; self.window.rootViewController =self.tabBarController; [self.window makeKeyAndVisible]; return YES; 
}

 

二、UITabBarItem

  UITabBar上面显示的每一个Tab都对应着一个ViewController,我们可以通过设置 viewcontroller.tabBarItem属性来改变tabbar上对应的tab显示内容。否则系统将会根据viewController的 title自动创建一个,该tabBarItem只显示文字,没有图像。当我们自己创建UITabBarItem的时候,我们可以显示的指定显示的图像和 对应的文字描述。当然还可以通过setFinishedSelectedImage:withFinishedUnselectedImage:方法给选中状态和飞选中状态指定不同的图片。下面看个自己创建UITabBarItem的小例子:

UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Second" image:nil tag:2]; [item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]]; viewController2.tabBarItem = item;
[item release];

  此外UITabBarItem还有一个属性badgeValue,通过设置该属性可以在其右上角显示一个小的角标,通常用于提示用户有新的消息,使用很简单,后面有例子。

 

三、moreNavigationController

  UITabBar上最多可以显示5个Tab,当我们往UITabBarController中添加超过的viewController超过5个时候,最后一个一个就会自动变成, 按照设置的viewControlles的顺序,显示前四个viewController的tabBarItem,后面的tabBarItem将不再显 示。当点击more时候将会弹出一个标准的navigationViewController,里面放有其它未显示的的viewController,并 且带有一个edit按钮,通过点击该按钮可以进入类似与ipod程序中设置tabBar的编辑界面。编辑界面中默认所有的viewController都 是可以编辑的,我们可以通过设置UITabBarController的customizableViewControllers属性来指定 viewControllers的一个子集,即只允许一部分viewController是可以放到tabBar中显示的。但是这块儿要注意一个问题就是 每当UITabBarController的viewControllers属性发生变化的时 候,customizableViewControllers就会自动设置成跟viewControllers一致,即默认的所有的 viewController都是可以编辑的,如果我们要始终限制只是某一部分可编辑的话,记得在每次viewControlles发生改变的时候,重新 设置一次customizableViewControllers。

  

四、UITabBarController的Rotation

  UITabBarController 默认只支持竖屏,当设备方向放生变化时候,它会查询viewControllers中包含的所有ViewController,仅当所有的 viewController都支持该方向时,UITabBarController才会发生旋转,否则默认的竖向。

  此处需要注意当UITabBarController支持旋转,而且发生旋转的时候,只有当前显示的viewController会接收到旋转的消息。

 

五、UITabBar

   UITabBar自己有一些方法是可以改变自身状态,但是对于UITabBarController自带的tabBar,我们不能直接去修改其状态。任何直接修改tabBar的操作将会抛出异常,下面看一个抛出异常的小例子:


self.tabBarController = [[[UITabBarController alloc] init] autorelease]; self.tabBarController.delegate = self; 
self.tabBarController.viewControllers =[NSArray arrayWithObjects:viewController1, viewController2, viewController3, nil]; self.window.rootViewController = self.tabBarController; 
[self.window makeKeyAndVisible]; 
self.tabBarController.tabBar.selectedItem = nil;

  上面代码的最后一行直接修改了tabBar的状态,运行程序回得到如下结果:


    下面为改变UITabBar选中和不选中时背景图片的举例:

     

     //初始化TabBar

    tabBarController = [[UITabBarController alloc] init];

    tabBarController.tabBar.selectionIndicatorImage = [UIImage                          imageNamed:@"select.png"];


    //初始化tabBarItems

UITabBarItem *tabBarItemOne = [[UITabBarItem alloc] initWithTitle:@"微信" image:nil tag:1];

      [tabBarItemOne setFinishedSelectedImage:[UIImage      imageNamed:@"tab_weixin_pressed.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_weixin_normal.png"]];

       UITabBarItem *tabBarItemTwo = [[UITabBarItem alloc] initWithTitle:@"通讯录" image:nil tag:1];

    [tabBarItemTwo setFinishedSelectedImage:[UIImage imageNamed:@"tab_address_pressed.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_address_normal.png"]];

    UITabBarItem *tabBarItemThree = [[UITabBarItem alloc] initWithTitle:@"朋友" image:nil tag:1];

    [tabBarItemThree setFinishedSelectedImage:[UIImage imageNamed:@"tab_find_frd_pressed.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_find_frd_normal.png"]];

    UITabBarItem *tabBarItemFour = [[UITabBarItem alloc] initWithTitle:@"设置" image:nil tag:1];

    [tabBarItemFour setFinishedSelectedImage:[UIImage imageNamed:@"tab_settings_pressed.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_settings_normal.png"]];


    //微信主视图

     MessageViewController *messageVC = [[MessageViewController alloc] initWithNibName:@"MessageViewController" bundle:nil];

    

    UINavigationController *lsNavi = [[UINavigationController alloc] initWithRootViewController:messageVC];

    lsNavi.view.center = CGPointMake(lsNavi.view.center.x,220);


    //设置navigationBar的标题和颜色

    messageVC.navigationItem.title =@"微信";

    [lsNavi.navigationBar setTintColor:[UIColor blackColor]];

    messageVC.tabBarItem = tabBarItemOne;

    [tabBarItemOne release];


    //通讯录主视图

    addressViewController *addressVC = [[addressViewController alloc] initWithNibName:@"addressViewController" bundle:nil];

    UINavigationController *asNavi = [[UINavigationController alloc] initWithRootViewController:addressVC];

    asNavi.view.center =  asNavi.view.center = CGPointMake(asNavi.view.center.x,220);


   //设置navigationBar的标题和颜色

    addressVC.navigationItem.title =@"通讯录";

    [asNavi.navigationBar setTintColor:[UIColor blackColor]];

    addressVC.tabBarItem =tabBarItemTwo;

    [tabBarItemTwo release];



   //朋友们主视图

    FriendViewController *friendVC = [[FriendViewController alloc] initWithNibName:@"FriendViewController" bundle:nil];

    UINavigationController *fdNavi = [[UINavigationController alloc] initWithRootViewController:friendVC];

    fdNavi.view.center =  fdNavi.view.center = CGPointMake(fdNavi.view.center.x,220);


   //设置navigationBar的标题和颜色

    friendVC.navigationItem.title =@"朋友们";

    [fdNavi.navigationBar setTintColor:[UIColor blackColor]];

    friendVC.tabBarItem = tabBarItemThree;

    [tabBarItemThree release];



     //设置主视图

    SettingViewController *settingVC = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];

    UINavigationController *stNavi = [[UINavigationController alloc] initWithRootViewController:settingVC];

    stNavi.view.center =  stNavi.view.center = CGPointMake(stNavi.view.center.x,220);


    //设置navigationBar的标题和颜色

    settingVC.navigationItem.title =@"设置";

    [stNavi.navigationBar setTintColor:[UIColor blackColor]];

    settingVC.tabBarItem = tabBarItemFour;

    [tabBarItemFour release];



六、Change Selected Viewcontroller

  改变UITabBarController中当前显示的viewController,可以通过一下两种方法:

  1、selectedIndex属性

  通过该属性可以获得当前选中的viewController,设置该属性,可以显示viewControllers中对应的index的 viewController。如果当前选中的是MoreViewController的话,该属性获取出来的值是NSNotFound,而且通过该属性 也不能设置选中MoreViewController。设置index超出viewControllers的范围,将会被忽略。

  2、selectedViewController属性

  通过该属性可以获取到当前显示的viewController,通过设置该属性可以设置当前选中的viewController,同时更新selectedIndex。可以通过给该属性赋值

tabBarController.moreNavigationController可以选中moreViewController。

  3、viewControllers属性

  设置viewControllers属性也会影响当前选中的viewController,设置该属性时 UITabBarController首先会清空所有旧的viewController,然后部署新的viewController,接着尝试重新选中上 一次显示的viewController,如果该viewController已经不存在的话,会接着尝试选中index和selectedIndex相 同的viewController,如果该index无效的话,则默认选中第一个viewController。

 

七、UITabBarControllerDelegate

  通过代理我们可以监测UITabBarController的当前选中viewController的变化,以及moreViewController中对编辑所有viewController的编辑。通过实现下面方法:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;

  该方法用于控制TabBarItem能不能选中,返回NO,将禁止用户点击某一个TabBarItem被选中。但是程序内部还是可以通过直接setSelectedIndex选中该TabBarItem。

  下面这三个方法主要用于监测对moreViewController中对view controller的edit操作

- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers; 
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;
 - (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微信话费充多了怎么办 睫毛烫的太卷了怎么办 烫完睫毛太卷了怎么办 烫睫毛太卷了怎么办 用微信充话费充错了怎么办 微信给空号充话费了怎么办 微信充话费充错号码是空号怎么办 淘宝充流量不到账怎么办 微信退货不退款怎么办 京东话费交错号怎么办? 微信缴费错了怎么办 给手机充话费被退款怎么办 买到假货淘宝商家已关店怎么办 手机刷错系统了怎么办 苹果手机成砖了怎么办 苹果6p变砖头怎么办 苹果刷成石头了怎么办 苹果手机更新成了砖头怎么办 京东售后好慢怎么办 京东商品超过售后期怎么办 京东过了售后期怎么办 京东售后不处理怎么办 京东售后不让退货怎么办 天猫盒子遥控器丢了怎么办 淘宝店铺的客服不理人怎么办 淘宝假货下架了怎么办 淘宝不让发布本地生活服务了怎么办 淘宝删除差评后店家不返现怎么办 天猫店家迟迟不发货怎么办 淘宝下单后店家说缺货怎么办 用淘宝把话费冲到空号上怎么办 d速快递没有网点怎么办 京东买的货没收到怎么办 淘宝物流显示已揽件就是不动怎么办 淘宝查不到物流信息怎么办 快递物流信息更新错怎么办 淘宝上查不到物流怎么办 微信买的东西不给退怎么办 微信购物已收货怎么办 微信买东西不退怎么办 银行经营贷款资金回流怎么办