您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

UITabBarController 完整用法:建立、代理、切換與 UITabBarItem 客製

編輯:iOS 開發綜合

UITabBarController 幾乎是所有多頁式 App 的骨架。以下把建立流程、常用代理方法、切換畫面的三種方式,以及 UITabBarItem 的客製化整理成一份可直接參照的筆記。


一、建立步驟


  1. 建立一個 UITabBarController
  2. 為每個分頁建立一個視圖控制器
  3. 建立一個 array,把視圖控制器都加進去,再把 array 指派給 tabBarController 的 viewControllers 屬性
  4. 把根視圖設為這個 tabBarController

兩個容易忽略的行為:


  • 只有在「支援旋轉」的前提下才會旋轉,且發生旋轉時只有當前顯示的 ViewController 會收到旋轉訊息
  • UITabBar 自帶的 tabBar 不能直接修改其內部結構

若要換背景圖,可以用這段做能力檢測後再設定:


UIImage *tabBackground = [UIImage imageNamed:@"main_title2.png"];
if ([tabBarController.tabBar respondsToSelector:@selector(setBackgroundImage:)]){
    [tabBarController.tabBar setBackgroundImage:tabBackground];
}

二、常用代理方法


點擊分頁時:


- (void)tabBarController:(UITabBarController *)tabBarController
   didSelectViewController:(UIViewController *)viewController {
    NSLog(@"%d", viewController.tabBarItem.tag);
}

透過代理我們可以監測目前選中 viewController 的變化,以及 moreViewController 中對所有 viewController 的編輯操作。


控制某個分頁能否被選中:


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

回傳 NO 將禁止使用者點擊某個 TabBarItem,但程式內部仍然可以透過直接設定 selectedIndex 選中它。


監測 moreViewController 的編輯操作:


- (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;

三、改變當前顯示的 viewController


1. selectedIndex 屬性


可取得目前選中的 viewController;設定該屬性會顯示 viewControllers 中對應 index 的項目。若當前選中的是 MoreViewController,此屬性會回傳 NSNotFound,而且無法用這個屬性去選中 MoreViewController。設定超出範圍的 index 會被忽略。


2. selectedViewController 屬性


可取得目前顯示的 viewController;設定它會同時更新 selectedIndex。若要選中 MoreViewController,可以對這個屬性賦值 tabBarController.moreNavigationController。


3. viewControllers 屬性


設定 viewControllers 也會影響當前選中的項目。UITabBarController 會先清空所有舊的 viewController,部署新的,接著嘗試重新選中上一次顯示的那個;若該物件已不存在,則嘗試選中相同 index 的項目;若該 index 無效,則預設選中第一個。


四、UITabBarItem 客製化


// 小红圈提示(badge)
barItem1.badgeValue = @"1";

// 為選中與未選中狀態指定不同圖片
[item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"]
       withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]];

每個 UIController 都有一個 navigationItem 和一個 tabBarItem,一般做法是建立對應類型的物件再賦值。


系統樣式:


UITabBarItem *barItem1 = [[UITabBarItem alloc]
    initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];
barItem1.badgeValue = @"1";
yvCtrl.tabBarItem = barItem1;

自訂樣式:


firstController.tabBarItem = [[UITabBarItem alloc]
    initWithTitle:@"First"
            image:[UIImage imageNamed:@"img.png"]
              tag:101];

也可以在 ViewController 的 init 裡建立:


(id)init {
    if ([super init] != nil) {
        UITabBarItem *item = [[UITabBarItem alloc]
            initWithTitle:@"asdfsadf"
                    image:[UIImage imageNamed:@"WWAN5.png"]
                      tag:1];
        self.tabBarItem = item;
        [item release];
    }
    return self;
}

五、完整建立範例


AppDelegate.h:


#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
{
    UITabBarController* tabBarController;
}

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) UITabBarController* tabBarController;

-(void) initTabBarController;

@end

AppDelegate.m:


-(void) initTabBarController
{
    // 建立 tab bar controller
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.delegate = self;

    // 1 ===> 首頁
    HomeController *homeView = [[HomeController alloc] initWithNibName:@"HomeController"
                                                              bundle:nil];
    UINavigationController *navhomeController =
        [[UINavigationController alloc] initWithRootViewController:homeView];
    [homeView release];
    [navhomeController.navigationBar customed];
    [navhomeController.navigationBar customedBarBackground];
    navhomeController.navigationBar.topItem.title = @"文件查看";
    navhomeController.tabBarItem = [[UITabBarItem alloc]
        initWithTitle:@"文件查看" image:[UIImage imageNamed:@"home.png"] tag:0];

    // 2 ===> 最近
    TempViewController *temp1 = [[TempViewController alloc]
        initWithNibName:@"TempViewController" bundle:nil];
    UINavigationController *navTempController =
        [[UINavigationController alloc] initWithRootViewController:temp1];
    [temp1 release];
    navTempController.navigationBar.topItem.title = @"最近";
    navTempController.tabBarItem = [[UITabBarItem alloc]
        initWithTitle:@"最近" image:[UIImage imageNamed:@"icon_bookmark.png"] tag:1];

    // 3 ===> 桌面
    DesktopViewController *desktopView = [[DesktopViewController alloc]
        initWithNibName:@"DesktopViewController" bundle:nil];
    UINavigationController *navDesktopView =
        [[UINavigationController alloc] initWithRootViewController:desktopView];
    [desktopView release];
    [navDesktopView.navigationBar customed];
    navDesktopView.navigationBar.topItem.title = @"桌面";
    navDesktopView.tabBarItem = [[UITabBarItem alloc]
        initWithTitle:@"桌面" image:[UIImage imageNamed:@"icon_attention.png"] tag:2];

    // 4 ===> 搜尋
    TempViewController *temp3 = [[TempViewController alloc]
        initWithNibName:@"TempViewController" bundle:nil];
    UINavigationController *navTempController3 =
        [[UINavigationController alloc] initWithRootViewController:temp3];
    [temp3 release];
    navTempController3.navigationBar.topItem.title = @"搜尋";
    navTempController3.tabBarItem = [[UITabBarItem alloc]
        initWithTitle:@"搜尋" image:[UIImage imageNamed:@"icon_search.png"] tag:3];

    // 5 ===> 設定
    SettingController *settingView = [[SettingController alloc]
        initWithNibName:@"SettingController" bundle:nil];
    UINavigationController *navSettingView =
        [[UINavigationController alloc] initWithRootViewController:settingView];
    [settingView release];
    navSettingView.navigationBar.topItem.title = @"設定";
    navSettingView.tabBarItem = [[UITabBarItem alloc]
        initWithTitle:@"設定" image:[UIImage imageNamed:@"icon_setting.png"] tag:4];

    // 把所有分頁交給 tab bar controller
    tabBarController.viewControllers = [NSArray arrayWithObjects:
        navhomeController, navTempController, navDesktopView,
        navTempController3, navSettingView, nil];

    OCRELEASE(navhomeController);
    OCRELEASE(navTempController);
    OCRELEASE(navDesktopView);
    OCRELEASE(navTempController3);
    OCRELEASE(navSettingView);
}

@synthesize tabBarController;

- (void)dealloc
{
    [_window release];
    [tabBarController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    [self initTabBarController];

    self.window.rootViewController = tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

這份範例中每個分頁都各自包了一層 UINavigationController,這是實務上最常見的組合:TabBarController 負責分頁切換,各分頁內部的 NavigationController 負責自己的堆疊式導覽。兩者分工清楚,不要試圖把 UINavigationController 直接放進 viewControllers 之外的地方,也不要在同一個 TabBarController 裡混用兩種模式。


另外提醒:程式碼中的 OCRELEASE 是專案自訂的巨集,通常展開後就是 [obj release] 加上歸零。在 ARC 環境下這些 release 都應該刪除。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved