首页 > 其他分享 >UITabBarController 标签栏控制器-IOS开发

UITabBarController 标签栏控制器-IOS开发

时间:2022-12-27 18:38:13浏览次数:45  
标签:控制器 标签 print IOS 视图 tabBarController UITabBarController view


在 UIKit 中UITabbar 代表了标签栏,而 UITabBarController 对其进行了封装,令多个不同的视图管理与切换变的更加轻松。

构建一个标签栏控制器,首先要为每个按钮准备一个单独的页。每一页都应被创建为UIViewController对象。

构建一个控制器数组:

你的应用程序可能有多个不同的试图控制器,来实现不同的功能。如果你在写一个音乐播放器,可能会有一些控制器,如:MusicList、CurrentPlay、Favourite、SingerList、Settings 等。在创建你的标签栏之前,你应该首先创建一个数组,在其中放置你希望在标签栏中显示的视图控制器对象。

 



[java]  ​​view plain​​ ​​copy​​ ​​print​​ ​​?​​



  1.  //生成各个视图控制器  
  2.     MusicList* musicList = [[[MusicList alloc]init]autorelease];  
  3.     CurrentPlay* currentPlay = [[[CurrentPlay alloc]init]autorelease];  
  4.     Favourite* favourite = [[[Favourite alloc]init]autorelease];  
  5.     SingerList* singerList = [[[SingerList alloc]init]autorelease];  
  6.     Settings* settings = [[[Settings alloc]initWithStyle:UITableViewStyleGrouped]autorelease];  
  7. //加入一个数组      
  8.     NSArray* controllerArray = [[NSArray alloc]initWithObjects:musicList,currentPlay,favourite,singerList,settings ,nil];  



配置按钮属性:

 

每个标签栏都有他自己的“标签”,定义了他的标签按钮是什么样子。在视图控制器的 init 方法中,可以配置标签栏按钮,定义视图的标题与/或 tabBarItem 属性:



[java]  ​​view plain​​ ​​copy​​ ​​print​​ ​​?​​



  1. - (id)initWithStyle:(UITableViewStyle)style{  
  2. super initWithStyle:style];  
  3. if (self) {  
  4. "Settings" image:[UIImage imageNamed:@"Setting"] tag:4];  
  5.     }  
  6. return self;  
  7. }  



请将 tabBarItem 属性设置为一个 UITabBarItem 对象。你有两种方法可以初始化标签栏中的项目。一种是initWithTitle 可以让你自定义标题和图像等数据来显示按钮。另一种就是创建系统提供的按钮。后者如下:



[html]  ​​view plain​​ ​​copy​​ ​​print​​ ​​?​​



  1. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  2. {  
  3. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  4.     if (self) {  
  5. self.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2];  
  6.     }  
  7.     return self;  
  8. }  



系统提供的按钮如下:



[html]  ​​view plain​​ ​​copy​​ ​​print​​ ​​?​​



  1. typedef enum {  
  2.     UITabBarSystemItemMore,  
  3.     UITabBarSystemItemFavorites,  
  4.     UITabBarSystemItemFeatured,  
  5.     UITabBarSystemItemTopRated,  
  6.     UITabBarSystemItemRecents,  
  7.     UITabBarSystemItemContacts,  
  8.     UITabBarSystemItemHistory,  
  9.     UITabBarSystemItemBookmarks,  
  10.     UITabBarSystemItemSearch,  
  11.     UITabBarSystemItemDownloads,  
  12.     UITabBarSystemItemMostRecent,  
  13.     UITabBarSystemItemMostViewed,  
  14. } UITabBarSystemItem;  



显示标签栏控制器:

 

标签栏所需的各个控制器都好了,现在就可以生成我们的标签栏控制器了。忘了讲了,控制器我是在

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary

 



[java]  ​​view plain​​ ​​copy​​ ​​print​​ ​​?​​



  1. //创建UITabBarController控制器    
  2.     UITabBarController* tabBarController = [[UITabBarController alloc]init];  
  3. //设置UITabBarController控制器的viewControllers属性为我们之前生成的数组controllerArray  
  4.     tabBarController.viewControllers = controllerArray;  
  5. //    默认选择第2个视图选项卡(索引从0开始的)  
  6. 1;  
  7. //    把tabBarController的view作为子视图添加到window  
  8.     [self.window addSubview:tabBarController.view];  



可定制按钮
默认情况下,当按钮多于5个时,标签栏控制器允许拥护对按钮布局进行定制。要做到这一点,可以单击标有“更多”的标签,然后单击随之出现的导航栏上的编辑按钮。你可以选择只对某些特定的标签进行定制,也可以完全禁止定制。要允许定制,请将标签栏控制器的 customizableViewControllers 设置为一个数组,数组中包含有你希望用户进行定制的试图控制器:

 

导航

当标签栏控制器被显示时,控制器自己处理导航操作,会将选中标签对应视图自动切换到屏幕前端。要读取或者更改当前活动的试图控制器,可以使用 selectedViewController 属性:



[java]  ​​view plain​​ ​​copy​​ ​​print​​ ​​?​​



  1. tabBarController.selectedViewController = musicList;  
  2. //读取  
  3.   UIViewController* activeController = tabBarController.selectedViewController;  
  4. if(activeController == musicList){  
  5. //  
  6.   }  



也可以使用索引:



[java]  ​​view plain​​ ​​copy​​ ​​print​​ ​​?​​



  1. tabBarController.selectedIndex = 1;  



委托代理

 

要在标签栏上的视图被选中时得到通知,请赋予标签栏控制器一个委托:

 



[java]  ​​view plain​​ ​​copy​​ ​​print​​ ​​?​​



  1. tabBarController.delegate = self;  



 委托会在选中一个tab时得到通知,然后 didSelectViewController 的委托方法会被调用:



[java]  ​​view plain​​ ​​copy​​ ​​print​​ ​​?​​



  1. - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{  
  2. /*添加代码,处理定制标签栏结束之后的操作*/  
  3. }  



至此结束,最后附上代码工程文件。
​UITabBarViewControllerDemo​​

标签:控制器,标签,print,IOS,视图,tabBarController,UITabBarController,view
From: https://blog.51cto.com/u_3457306/5973146

相关文章

  • sqlite 实例教程 IOS下用sqlite打造词典-IOS开发
    sqlite是个好东西,对于移动平台来说。一直想写有关sqlite的教程,但是不知道从何写起,考虑了很久,还是从一个小Demo谈起吧。我写了一个精简版的词典,实现了增删查改的基本功能。......
  • iOS中登錄功能的體驗探究
    登錄功能是我在​​湖畔​​做的第一個需求。 當時PD给我的草圖和下圖類似:(圖片來自知乎iOS客戶端登錄界面) 不過需求中要求用戶名或者密碼錯誤時,輸入框要抖動(類似Mac登錄密......
  • IOS 6 自动布局 入门-1
    这篇文章还可以在这里找到 ​​英语​​​, ​​韩语​​​, ​​土耳其语​​​​​​来自Ray:恭喜各位!你们已经通过宣传​​iosfeast​​提前解锁了第一个有关IOS6的教......
  • iOS学习笔记45—本地通知UILocalNotification
    在iOS中有两类信息提示推送方式,一类是远程服务器推送(APNS),之前有笔记9​​,还有一类就是本地通知UILocalNotification,今天就简要的记录一下UILocalNotification的使用,代码里见......
  • iOS6下自定义UI控件外观效果
    尽管iOS原生的UI控件就已经有很不错的显示效果,但是App开发者仍然希望自己的产品与众不同,所以自定义UI外观成了每个App产品开发必做之事。今天就来做一个在iOS6下实现自定义U......
  • IOS中Json解析的四种方法
    作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式。有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验(​​......
  • iOS多线程编程之NSThread的使用
    1、简介:1.1iOS有三种多线程编程的技术,分别是:1.、​​NSThread​​ 2、​​CocoaNSOperation​​ (​​iOS多线程编程之NSOperation和NSOperationQueue的使用​​)3、​​G......
  • iOS第三方开源库的吐槽和备忘
    做iOS开发总会接触到一些第三方库,这里整理一下,做一些吐槽。 目前比较活跃的社区仍旧是Github,除此以外也有一些不错的库散落在GoogleCode、SourceForg......
  • iOS 开发者必不可少的 75 个工具
    如果你去到一位熟练的木匠的工作室,你总是能发现他/她有一堆工具来完成不同的任务。软件开发同样如此。你可以从软件开发者如何使用工具中看出他水准如何。有经验的开发者精......
  • 【iOS知识学习】_iOS开源项目汇总
    扫描wifi信息:​​http://code.google.com/p/uwecaugmentedrealityproject/​​​​http://code.google.com/p/iphone-wireless/​​条形码扫描:​​http://zbar.sourceforge.......