1. 程式人生 > >UITabBarController 標籤欄控制器-IOS開發

UITabBarController 標籤欄控制器-IOS開發

在 UIKit 中UITabbar 代表了標籤欄,而 UITabBarController 對其進行了封裝,令多個不同的檢視管理與切換變的更加輕鬆。

構建一個標籤欄控制器,首先要為每個按鈕準備一個單獨的頁。每一頁都應被建立為UIViewController物件。

構建一個控制器陣列:

你的應用程式可能有多個不同的試圖控制器,來實現不同的功能。如果你在寫一個音樂播放器,可能會有一些控制器,如:MusicList、CurrentPlay、Favourite、SingerList、Settings 等。在建立你的標籤欄之前,你應該首先建立一個數組,在其中放置你希望在標籤欄中顯示的檢視控制器物件。

 //生成各個檢視控制器
    MusicList* musicList = [[[MusicList alloc]init]autorelease];
    CurrentPlay* currentPlay = [[[CurrentPlay alloc]init]autorelease];
    Favourite* favourite = [[[Favourite alloc]init]autorelease];
    SingerList* singerList = [[[SingerList alloc]init]autorelease];
    Settings* settings = [[[Settings alloc]initWithStyle:UITableViewStyleGrouped]autorelease];
//加入一個數組    
    NSArray* controllerArray = [[NSArray alloc]initWithObjects:musicList,currentPlay,favourite,singerList,settings ,nil];
配置按鈕屬性:

每個標籤欄都有他自己的“標籤”,定義了他的標籤按鈕是什麼樣子。在檢視控制器的 init 方法中,可以配置標籤欄按鈕,定義檢視的標題與/或 tabBarItem 屬性:

- (id)initWithStyle:(UITableViewStyle)style{
    self = [super initWithStyle:style];
    if (self) {
    self.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"Settings" image:[UIImage imageNamed:@"Setting"] tag:4];
    }
    return self;
}
請將 tabBarItem 屬性設定為一個 UITabBarItem 物件。你有兩種方法可以初始化標籤欄中的專案。一種是initWithTitle 可以讓你自定義標題和影象等資料來顯示按鈕。另一種就是建立系統提供的按鈕。後者如下:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
      self.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2];
    }
    return self;
}
系統提供的按鈕如下:
typedef enum {
    UITabBarSystemItemMore,
    UITabBarSystemItemFavorites,
    UITabBarSystemItemFeatured,
    UITabBarSystemItemTopRated,
    UITabBarSystemItemRecents,
    UITabBarSystemItemContacts,
    UITabBarSystemItemHistory,
    UITabBarSystemItemBookmarks,
    UITabBarSystemItemSearch,
    UITabBarSystemItemDownloads,
    UITabBarSystemItemMostRecent,
    UITabBarSystemItemMostViewed,
} UITabBarSystemItem;
顯示標籤欄控制器:

標籤欄所需的各個控制器都好了,現在就可以生成我們的標籤欄控制器了。忘了講了,控制器我是在

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  中生成的。

//建立UITabBarController控制器  
    UITabBarController* tabBarController = [[UITabBarController alloc]init];
//設定UITabBarController控制器的viewControllers屬性為我們之前生成的陣列controllerArray
    tabBarController.viewControllers = controllerArray;
//    預設選擇第2個檢視選項卡(索引從0開始的)
    tabBarController.selectedIndex = 1;
//    把tabBarController的view作為子檢視新增到window
    [self.window addSubview:tabBarController.view];
可定製按鈕
預設情況下,當按鈕多於5個時,標籤欄控制器允許擁護對按鈕佈局進行定製。要做到這一點,可以單擊標有“更多”的標籤,然後單擊隨之出現的導航欄上的編輯按鈕。你可以選擇只對某些特定的標籤進行定製,也可以完全禁止定製。要允許定製,請將標籤欄控制器的 customizableViewControllers 設定為一個數組,陣列中包含有你希望使用者進行定製的試圖控制器:

導航

當標籤欄控制器被顯示時,控制器自己處理導航操作,會將選中標籤對應檢視自動切換到螢幕前端。要讀取或者更改當前活動的試圖控制器,可以使用 selectedViewController 屬性:

  tabBarController.selectedViewController = musicList;
    //讀取
    UIViewController* activeController = tabBarController.selectedViewController;
    if(activeController == musicList){
        //
    }
也可以使用索引:
tabBarController.selectedIndex = 1;
委託代理

要在標籤欄上的檢視被選中時得到通知,請賦予標籤欄控制器一個委託:

tabBarController.delegate = self;
 委託會在選中一個tab時得到通知,然後 didSelectViewController 的委託方法會被呼叫:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    /*新增程式碼,處理定製標籤欄結束之後的操作*/
}
至此結束,最後附上程式碼工程檔案。
UITabBarViewControllerDemo