1. 程式人生 > >IOS:標籤欄與導航欄

IOS:標籤欄與導航欄

1、在 UIKit 中UITabbar 代表了標籤欄,而 UITabBarController 對其進行了封裝,令多個不同的檢視管理與切換變的更加輕鬆。構建一個標籤欄控制器,首先要為每個按鈕準備一個單獨的頁。每一頁都應被建立為UIViewController物件。

  

   首先建立一個單檢視工程,然後在建兩個新類,他們都繼承自UIViewController,分別取名為firstViewController,secondViewController。

   在AppDelegate.h中程式碼如下:

import <UIKit/UIKit.h>

#import "firstViewController.h"

#import "secondViewController.h"

@classViewController;

@interface AppDelegate :UIResponder <UIApplicationDelegate>

@property (strong,nonatomic)UIWindow *window;

@property (strong,nonatomic)ViewController *viewController;

@property(retain,nonatomic)firstViewController *pFirst;

@property(

retain,nonatomic)secondViewController *pSecond;

@end

   在AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中新增如下程式碼:  

//建立初始化檢視控制器

firstViewController *pfirstVC = [[firstViewControlleralloc]initWithNibName:nilbundle:nil

];

secondViewController *psecondVC = [[secondViewControlleralloc]initWithNibName:nilbundle:nil];

   self.pFirst = pfirstVC;

   self.pSecond = psecondVC;

    [pfirstVCrelease];

    [psecondVCrelease];

//建立UITabBarController物件

UITabBarController *pBar = [[UITabBarControlleralloc]init];

//建立陣列來存放多個試圖控制器物件

   NSArray *mArr = [NSArrayarrayWithObjects:self.pFirst,self.pSecond,nil];

//UITabBarController物件的viewControllers設定為數組裡的元素

    pBar.viewControllers = mArr;

//將當前視窗的根檢視控制器設為pBar

self.window.rootViewController = pBar;

    然後,我們可以在兩個兩類中分別對標籤欄進行自定義設定,如在firstViewController.m中的

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil方法中新增如下程式碼:

//設定標籤欄的格式和tag

self.tabBarItem = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarkstag:10];

//設定檢視背景色為紅色

self.view.backgroundColor = [UIColorredColor];

    同樣,在secondViewController.m中的-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil方法中新增如下程式碼:

//設定標籤欄的標題和背景圖片(名字為“10.png”)以及tag

self.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"設定"image:[UIImageimageNamed:@"10.png"] tag:20];

//設定檢視背景色為黃色

self.view.backgroundColor = [UIColoryellowColor];

  最後,執行結果:



  點選“設定”按鈕,檢視會轉換,             2、導航欄    同樣建立單檢視工程,在AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中新增如下程式碼:

//建立初始化導航控制器

UINavigationController *pNavigation = [[UINavigationControlleralloc]initWithRootViewController:self.viewController];

//將導航控制器物件設為視窗的根檢視控制器

   self.window.rootViewController = pNavigation;

   //釋放

    [pNavigationrelease];

    然後,我們可以在ViewController.m中的- (void)viewDidLoad新增如下程式碼

//設定導航欄標題

self.navigationItem.title =@"導航欄";

//設定導航欄上的按鈕

UIBarButtonItem *pBtn = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:selfaction:@selector(buttonAction:)];

//將按鈕新增到導航欄右邊

self.navigationItem.rightBarButtonItem = pBtn;

        和按鈕關聯的方法:

//按鈕關聯的動作

- (void)buttonAction:(id)sender

{

UIAlertView *pAlert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"You can breath easy!"delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];

    [pAlertshow];

}

     執行結果:
         點選“Done”按鈕,結果如下:          點選"OK"回返會上一個介面。   3、要將導航欄和標籤欄結合起來只需要在我們建立標籤欄的程式碼上新增並改動幾句程式碼,如下

UINavigationController *pNavi1 = [[UINavigationControlleralloc]initWithRootViewController:self.pFirst];

UINavigationController *pNavi2 = [[UINavigationControlleralloc]initWithRootViewController:self.pSecond];

//建立UITabBarController物件

UITabBarController *pBar = [[UITabBarControlleralloc]init];

//建立陣列來存放多個試圖控制器物件

    NSArray *mArr = [NSArrayarrayWithObjects:pNavi1,pNavi2,nil];

//UITabBarController物件的viewControllers設定為數組裡的元素

    pBar.viewControllers = mArr;

//將當前視窗的根檢視控制器設為pBar

self.window.rootViewController = pBar;

  執行結果

   

  可以看到,有導航欄生成,然後我們可以分別在每個類中對的導航類進行設定,即可。

   另外,我們經常可以看到在標籤欄上有一個包含數字的小紅圈,如圖:       這是tabBarItem的badValue屬性,只需要在初始化方法新增一句話即可實現:

self.tabBarItem.badgeValue =@"8";