IOS開發(fā) 分欄控制器基礎(chǔ)

分欄控制器的概念:

? ? ?分欄控制器是管理多個視圖控制器的管理控制器,通過數(shù)組的方式管理多個平行關(guān)系的視圖控制器,與導(dǎo)航控制器的區(qū)別在于:導(dǎo)航控制器管理的是有層級關(guān)系的控制器。

? 分欄控制器在同一界面最多顯示5個控制器切換按鈕,超過5個時會自動創(chuàng)建一個新的導(dǎo)航控制器來管理其余的控制器。

UITabBarltem:分欄按鈕元素對象

badgeValue:分欄按鈕提示信息

selectedIndex:分欄控制器選中的控制器索引

viewControllers:分欄控制器管理數(shù)組

selectedViewController:分欄控制器選中的控制器對象


1.創(chuàng)建VCFirst,VCSecond,VCThrid視圖都繼承于UIViewController

【VCFirst.m】

@interface VCFirst()

@end

@implementation VCFirst

-(void)viewDidLoad{

[super viewDidLoad];

//方法一

//創(chuàng)建一個分欄按鈕,參數(shù)1:文字,參數(shù)2:顯示圖片,參數(shù)3:設(shè)置按鈕的tag值

/*UITabBarItem* tabBarItem=[[UITabBarItem alloc] initWithTitle:@"" image:nil tag:101];

self.tabBarItem=tabBarItem;*/

方法二

//根據(jù)系統(tǒng)風(fēng)格創(chuàng)建分欄按鈕,參數(shù)1:系統(tǒng)風(fēng)格設(shè)定

UITabBarItem* tabBarItem=[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:101];

//按鈕右上角的提示信息,通常用來提示未讀的信息

tabBarItem.badgeValue=@"22";

self.tabBarItem=tabBarItem;

}






【AppDelegate.h】

#Import"AppDelegate.h"

#import"VCFirst.h"

#import"VCSecond.h"

#import"VCThird.h"

@interface AppDelegate()

@end

@implementation AppDelegate

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

//創(chuàng)建Window

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

//顯示window

[self.wind makeKeyAndVisible];

VCFirst* ?vcFirst=[[VCFirst allock]init];

vcFirst.view.backgroundcolor=[[UIColor blueColor];

//創(chuàng)建視圖控制器二

VCSecond* vcSecond=[[VCSecond alloc]init];

vcSecond.view.backgroundColor=[UIColor yellowColor];

VCThird* vcThird=[[VCThird alloc]init];

vcThird.view.backgroundColor=[UIColor orangeColor];

vcFirst.title=@"視圖1";

vcSecond.title=@"視圖2";

vcThird.title=@"視圖3";

UITabBarController* tbController=[[UITabBarController alloc]init];

//創(chuàng)建一個控制器數(shù)組對,將所有要被分欄制器管理的對象添加到數(shù)組中

NSArray* ?arrayVC=[NSArray arrayWithObjects:vcFirst,vcSecond,vcThrid.nil];

//將分欄視圖控制器管理數(shù)組賦值

tbController.viewControllers=arrayVC;

//將分欄控制器做為根視圖控制器

self.window.rootViewController=tbController;

//設(shè)置選中的視圖控器的索引,通過索引來確定顯示哪個一人控器,默認(rèn)值從0開始

tbController.selectedIndex=2;

if(tbController.selectedViewController==vcThird){

NSLog(@"當(dāng)前顯示的是控制器三~");

}

//設(shè)置分欄控制器的工具欄的透明度

tbController.tabBar.translucent=NO;

return YES;

}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • *7月8日上午 N:Block :跟一個函數(shù)塊差不多,會對里面所有的內(nèi)容的引用計數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,738評論 1 14
  • { 11、核心動畫 需要簽協(xié)議,但是系統(tǒng)幫簽好 一、CABasicAnimation 1、創(chuàng)建基礎(chǔ)動畫對象 CAB...
    CYC666閱讀 1,704評論 2 4
  • 代碼創(chuàng)建UIWindow對象 Xcode7之后使用代碼創(chuàng)建UIWindow對象: //創(chuàng)建UIWindow對象 s...
    云之君兮鵬閱讀 1,496評論 0 2
  • 42 函數(shù)的定義 函數(shù)就是方法一個小程序(特定功能),函數(shù)就是方法 修飾符 返回值類型 函數(shù)名(參數(shù)類型 形式參數(shù)...
    叢鵬閱讀 239評論 0 0
  • 公元591年,長孫大人自縊,滿朝的忠良去了大半。我在長安的酒樓點了一壺酒,看著市里來往的行人,和著滿腔的愁...
    公瑾當(dāng)年羽扇閱讀 1,147評論 0 1

友情鏈接更多精彩內(nèi)容