日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UITabBarController 基本用法

發布時間:2024/1/17 编程问答 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UITabBarController 基本用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

?

?

一、UITabBarController對象的創建和初始化

?

首先創建多個視圖控制器,并在tabBarItem中添加標題和圖片

?

???self.viewController.tabBarItem.title?=?@"首頁";//設置tabBarItem標題

????self.viewController.tabBarItem.image?= [UIImage?imageNamed:@"icon_home"];//添加圖片

????self.viewController.tabBarItem.badgeValue?=?@"1";//消息

????FirstViewController?*one = [[FirstViewController?alloc]?init];

????one.tabBarItem.title?=?@"消息";

????one.tabBarItem.image?= [UIImage?imageNamed:@"icon_meassage"];

????SecondViewController?*two = [[SecondViewController?alloc]?init];

????two.tabBarItem.title?=?@"聯系人";

????two.tabBarItem.image?= [UIImage?imageNamed:@"icon_selfinfo"];

????ThirdViewController?*three = [[ThirdViewController?alloc]?init];

???three.title?=?@"搜索";

????three.tabBarItem.image?= [UIImage?imageNamed:@"icon_search"];

????ForthViewController?*four = [[ForthViewController?alloc]?init];

????four.tabBarItem.title?=?@"更多";

????four.tabBarItem.image?= [UIImage?imageNamed:@"icon_more"];

????FifthViewController?*five = [[FifthViewController?alloc]init];

????five.tabBarItem.title?=?@"訂閱";

????SixthViewController?*six = [[SixthViewController?alloc]?init];

????six.tabBarItem.title?=?@"下載";

????

????UINavigationController?*nav = [[UINavigationController?alloc]initWithRootViewController:three];

?

??注意,第三個視圖控制器為導航控制器?

?

將多個視圖控制器添加到數組(視圖超過5個以后,自動生成More,隱藏多出來的視圖控制器)

效果為多出來下邊一排視圖切換工具



????NSArray?*vcArray = [NSArray?arrayWithObjects:self.viewController, one, two, nav, four, five, six,?nil];//創建視圖控制器數組?

????UITabBarController?*tabBarC = [[UITabBarController?alloc]?init];

????tabBarC.tabBar.selectedImageTintColor?= [UIColor?redColor];//選中時圖片顏色變為紅色

????tabBarC.viewControllers?= vcArray;//將多個視圖控制器組織到tab控制器中,以選項卡

?

???self.window.rootViewController?= tabBarC;//設置根視圖控制器為tabBarController

?

二、UITabBarController可以實現UITabBarControllerDelegate協議

?

tabBarC.delegate?=?self; //設置self為代理

可以重寫代理中方法(很少用),表示選中了某個ViewController

?

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

//執行相應操作

}

?

?

//代碼解說

- (BOOL)application:(UIApplication?*)application didFinishLaunchingWithOptions:(NSDictionary?*)launchOptions

{

????self.window?= [[[UIWindow?alloc]?initWithFrame:[[UIScreen?mainScreen]?bounds]]?autorelease];

????FirstViewController?*firstController=[[FirstViewController?alloc]?init];

????//設置標題

????firstController.title=@"First";

????//設置tabBarItem的樣式(這種方式是按照系統定義的方式)

????firstController.tabBarItem=[[UITabBarItem?alloc]?initWithTabBarSystemItem:UITabBarSystemItemMoretag:101];

????//也可以用自定義方式方式如下:

????

????

????SecondViewController?*secondController=[[SecondViewController?alloc]?init];

????secondController.title=@"Second";

????secondController.tabBarItem=[[UITabBarItem?alloc]?initWithTabBarSystemItem:UITabBarSystemItemHistorytag:102];

????ThirdViewController?*thirdController=[[ThirdViewController?alloc]?init];

????thirdController.title=@"Third";

????thirdController.tabBarItem=[[UITabBarItem?alloc]?initWithTabBarSystemItem:UITabBarSystemItemFavoritestag:103];

????

????UINavigationController?*nav1=[[UINavigationController?alloc]?initWithRootViewController:firstController];

????[firstController?release];

????UINavigationController?*nav2=[[UINavigationController?alloc]?initWithRootViewController:secondController];

????[secondController?release];

????UINavigationController?*nav3=[[UINavigationController?alloc]?initWithRootViewController:thirdController];

????[thirdController?release];

????NSArray?*controllersArray=[[NSArrayalloc]?initWithObjects:nav1,nav2,nav3,?nil];

????[nav1?release];

????[nav2?release];

????[nav3?release];

????

????UITabBarController?*tabController=[[UITabBarController?alloc]?init];

????tabController.viewControllers=controllersArray;

????tabController.selectedIndex=0;

????tabController.delegate=self;//在AppDelegate.h文件內實現UITabBarControllerDelegate協議

????self.tabBarController=tabController;

????[tabController?release];

????[controllersArray?release];

????

????[self.window?addSubview:tabController.view];

?

????[self.window?makeKeyAndVisible];

????returnYES;

}

?

//當點擊tabBarItem時觸發該操作??UITabBarControllerDelegate中的一個方法

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

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

}

?

轉載于:https://www.cnblogs.com/zfrankice/p/4238998.html

總結

以上是生活随笔為你收集整理的UITabBarController 基本用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。