1. 程式人生 > >ios開發 UITabBarController

ios開發 UITabBarController

1.建立一個UITableController

2.為每個Tab建立一個檢視控制器

3.建立一個array  把檢視控制器都新增到array中 在把array放到UITabBarCtrl的Controllers中

4.設定根檢視為tabBar

*只有在支援旋轉的設定  才會旋轉  發生旋轉時只有當前的ViewController才會接受到旋轉的訊息

*對於UITabBar自帶的tabBar  不能直接去修改

//修改背景圖片

UIImage *tabBackground = [UIImage imageNamed:@"main_title2.png"];

   if ([tabBarController.tabBar respondsToSelector:@selector(setBackgroundImage:)]){

        [tabBarController.tabBar setBackgroundImage:tabBackground];

    }

常用委託

//當點選tabBarItem時觸發該操作  

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *) viewController {

    NSLog(@"%d", viewController.tabBarItem.tag);

}

通過代理我們可以監測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;

改變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。

UITabBarItem

//小紅圈提示

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   在初始化裡新增Item

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

建立一個UITabConroller:

.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  

.m檔案中:
-(void) initTabBarController  
{  
   // create a tab bar controller  
   self.tabBarController = [[UITabBarControlleralloc]init];  
    self.tabBarController.delegate =self;  
   //1====>navhomeController  
    HomeController* homeView = [[HomeControlleralloc]initWithNibName :@"HomeController"bundle : nil];  
    UINavigationController* navhomeController = [[UINavigationControlleralloc]initWithRootViewController : homeView];  
    [homeView release];  
    [navhomeController.navigationBarcustomed];  
    [navhomeController.navigationBarcustomedBarBackground];  
    navhomeController.navigationBar.topItem.title [email protected]"檔案檢視";  
    navhomeController.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"檔案檢視" image:[UIImageimageNamed:@"home.png"] tag :0];  
      
   TempViewController* temp1 = [[TempViewControlleralloc]initWithNibName:@"TempViewController"bundle:nil];  
   UINavigationController* navTempController = [[UINavigationControlleralloc]initWithRootViewController : temp1];  
    [temp1 release];  
    navTempController.navigationBar.topItem.title [email protected]"最近";  
    navTempController.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"最近"image:[UIImageimageNamed:@"icon_bookmark.png"] tag :1];  
      
   DesktopViewController* desktopView = [[DesktopViewControlleralloc]initWithNibName:@"DesktopViewController"bundle:nil];  
    UINavigationController* navDesktopView = [[UINavigationControlleralloc]initWithRootViewController : desktopView];  
    [desktopView release];  
    [navDesktopView.navigationBarcustomed];  
    navDesktopView.navigationBar.topItem.title [email protected]"桌面";  
    navDesktopView.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"桌面"image:[UIImageimageNamed:@"icon_attention.png"] tag :2];  
      
   TempViewController* temp3 = [[TempViewControlleralloc]initWithNibName:@"TempViewController"bundle:nil];  
   UINavigationController* navTempController3 = [[UINavigationControlleralloc]initWithRootViewController : temp3];  
    [temp3 release];  
    navTempController3.navigationBar.topItem.title [email protected]"搜尋";  
    navTempController3.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"搜尋"image:[UIImageimageNamed:@"icon_search.png"] tag :3];  
      
      
   //5====>SettingController  
    SettingController* settingView = [[SettingControlleralloc]initWithNibName :@"SettingController"bundle : nil];  
    UINavigationController* navSettingView = [[UINavigationControlleralloc]initWithRootViewController : settingView];  
    [settingView release];  
    navSettingView.navigationBar.topItem.title [email protected]"設定";  
    navSettingView.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"設定"image:[UIImageimageNamed:@"icon_setting.png"] tag :4];  
      
    tabBarController.viewControllers = [NSArrayarrayWithObjects : navhomeController,navTempController,navDesktopView,navTempController3,navSettingView,nil];  
      
    OCRELEASE(navhomeController);  
    OCRELEASE(navTempController);  
    OCRELEASE(navDesktopView);  
    OCRELEASE(navTempController3);  
    OCRELEASE(navSettingView);  
      
   // add navigation controllers to the tab bar controller  
   //    tabBarController.viewControllers = [NSArray arrayWithObjects : navhomeController,navPersonalController,navAttentionView,navSeekView,navSettingView,nil];  
   //    OCRELEASE(navhomeController);  
   //    OCRELEASE(navPersonalController);  
   //    OCRELEASE(navAttentionView);  
   //    OCRELEASE(navSeekView);  
   //    OCRELEASE(navSettingView);  
}  
@synthesize tabBarController;  
  
- (void)dealloc  
{  
    [_windowrelease];  
    [tabBarControllerrelease];  
    [superdealloc];  
}  
  
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
{  
   self.window = [[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];  
   // Override point for customization after application launch.  
    [selfinitTabBarController];  
      
   self.window.rootViewController =tabBarController;  
    [self.windowmakeKeyAndVisible];  
   returnYES;  
}