你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> UITabBarController

UITabBarController

編輯:IOS開發綜合

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文件中:
[cpp] 
#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文件中:
[cpp] 
-(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 =@"文件查看"; 
    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 =@"最近"; 
    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 =@"桌面"; 
    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 =@"搜索"; 
    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 =@"設置"; 
    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];  www.2cto.com
     
   self.window.rootViewController =tabBarController; 
    [self.windowmakeKeyAndVisible]; 
   returnYES; 

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