UITabBarDelegate & UITabBarControllerDelegate詳解

今天在做項目的時候遇到了一個問題:怎么判斷進入一個(view)界面是tabbar左右切換顯現(xiàn)的還由詳情界面推出顯示的季稳,因為不同的顯示view的方式,我要進行不同的處理景鼠。所以,就研究了一下UITabBarDelegate & UITabBarControllerDelegate

UITabBarDelegate

TabBarDelegate 一共有五個代理方法溯香,而且代理方法必須寫在UITabBarController的控制器里面浓恶,不然代理方法不會執(zhí)行。直接在UITabBarController控制器里面寫<UITabBarDelegate>问顷,他就可以執(zhí)行代理方法

代理方法

[目前感覺就是第一個有用,其他的不知道干啥]
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSLog(@"選中了某個item");
}

- (void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray<UITabBarItem *> *)items{
    NSLog(@"將要開始自定制item");
}
- (void)tabBar:(UITabBar *)tabBar didBeginCustomizingItems:(NSArray<UITabBarItem *> *)items{
    NSLog(@"已經(jīng)開始自定制item");
}
- (void)tabBar:(UITabBar *)tabBar willEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed {
    NSLog(@"將要結(jié)束自定制item");
}
- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed{
    NSLog(@"將要結(jié)束自定制item");
}

UITabBarControllerDelegate

//設置控制器數(shù)組
@property(nullable, nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;
//設置控制器數(shù)組 動畫
- (void)setViewControllers:(NSArray<__kindof UIViewController *> * __nullable)viewControllers animated:(BOOL)animated;
//選中的控制器
@property(nullable, nonatomic, assign) __kindof UIViewController *selectedViewController; 
//選中索引值
@property(nonatomic) NSUInteger selectedIndex;
//當item超過五個時 就會有一個更多
@property(nonatomic, readonly) UINavigationController *moreNavigationController; 

@property(nullable, nonatomic, copy) NSArray<__kindof UIViewController *> *customizableViewControllers; 
//tab條
@property(nonatomic,readonly) UITabBar *tabBar NS_AVAILABLE_IOS(3_0); 
//委托
@property(nullable, nonatomic,weak) id<UITabBarControllerDelegate> delegate;

@end

1、在Application的中編碼塞耕,平時項目要使用繼承一個于UITabBarController的控制器里面

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

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

    //初始化一個tabBar控制器
    UITabBarController *tb = [[UITabBarController alloc]init];
    //設置UIWindow的rootViewController為UITabBarController
    self.window.rootViewController = tb;

    //創(chuàng)建相應的子控制器
    UIViewController *vc1 = [[UIViewController alloc]init];
    vc1.view.backgroundColor = [UIColor greenColor];
    vc1.tabBarItem.title = @"首頁";
    vc1.tabBarItem.image = [[UIImage imageNamed:@"Home_normal"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc1.tabBarItem.selectedImage = [[UIImage imageNamed:@"Home_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    UIViewController *vc2 = [[UIViewController alloc]init];
    vc2.view.backgroundColor = [UIColor blueColor];
    vc2.tabBarItem.title = @"分類";
    vc2.tabBarItem.image = [[UIImage imageNamed:@"List_normal"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc2.tabBarItem.selectedImage = [[UIImage imageNamed:@"List_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:102.0/255 green:102.0/255 blue:102.0/255 alpha:1.0],NSForegroundColorAttributeName, [UIFont systemFontOfSize:10.0],NSFontAttributeName,nil] forState:UIControlStateNormal];

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:255.0/255 green:73.0/255 blue:87.0/255 alpha:1.0],NSForegroundColorAttributeName, [UIFont systemFontOfSize:10.0],NSFontAttributeName,nil] forState:UIControlStateSelected];

    //把子控制器添加到UITabBarController
    //[tb addChildViewController:c1];
    //[tb addChildViewController:c2];
    //或者
    tb.viewControllers = @[vc1,vc2];
    [self.window makeKeyAndVisible];
    return YES;   
}

2莉钙、UITabBarControllerDelegate委托內(nèi)容

1筛谚、視圖將要切換時調(diào)用,viewController為將要顯示的控制器驾讲,如果返回的值為NO,則無法點擊其它分欄了(viewController指代將要顯示的控制器)
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{

   NSLog(@"被選中的控制器將要顯示的按鈕");
   //return NO;不能顯示選中的控制器
   return YES;

}
 2时迫、視圖已經(jīng)切換后調(diào)用谓晌,viewController 是已經(jīng)顯示的控制器

 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
  NSLog(@"視圖顯示后調(diào)用");
}
 3、將要開始自定義item的順序

- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers
{

      NSLog(@"將要開始自定義item時調(diào)用");

      NSLog(@"%@",viewControllers);
}
 4纸肉、將要結(jié)束自定義item的順序

 - (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed

{

       NSLog(@"將要結(jié)束自定義item時調(diào)用");

       NSLog(@"%@",viewControllers);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市胧奔,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌胳泉,老刑警劉巖岩遗,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異案铺,居然都是意外死亡梆靖,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進店門姑子,熙熙樓的掌柜王于貴愁眉苦臉地迎上來测僵,“玉大人街佑,你說我怎么就攤上這事捍靠。” “怎么了磁携?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵良风,是天一觀的道長。 經(jīng)常有香客問我,道長这吻,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任怠硼,我火速辦了婚禮,結(jié)果婚禮上香璃,老公的妹妹穿的比我還像新娘。我一直安慰自己姻乓,他們只是感情好眯牧,可當我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著学少,像睡著了一般。 火紅的嫁衣襯著肌膚如雪扣囊。 梳的紋絲不亂的頭發(fā)上绒疗,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天,我揣著相機與錄音盒至,去河邊找鬼。 笑死枷遂,一個胖子當著我的面吹牛棋嘲,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播沸移,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼雹锣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蕊爵?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤醋旦,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后饲齐,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡御雕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年先慷,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片福青。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡脓诡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出祝谚,到底是詐尸還是另有隱情,我是刑警寧澤次泽,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布席爽,位于F島的核電站,受9級特大地震影響只锻,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜捐寥,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一祖驱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧捺僻,春花似錦、人聲如沸就珠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽泞歉。三九已至,卻和暖如春腰耙,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背晰赞。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工选侨, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人援制。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓晨仑,卻偏偏與公主長得像褐墅,于是被迫代替她去往敵國和親洪己。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫猾封、插件噪珊、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,094評論 4 62
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,077評論 25 707
  • “上午的工作時間到了痢站,大家可以休息了,感覺時間真的過得好快啊阵难。”主編分明溫和卻因室內(nèi)的安靜顯得洪亮的聲音快速穿過被...
    菠蘿啦閱讀 253評論 0 0
  • 古代與現(xiàn)代的社交異同 人只要是社會的人盛泡,只要活在這個社會上一天,就總會有各種各樣的社交傲诵。但是,有一些社交是根本沒...
    耐心的獵人閱讀 541評論 0 0
  • 怎么做紅燒魚悟衩?油炸栓拜?煎座泳?魚皮要掉菱属?有魚腥味?味道不香纽门?這些都是大家在做家常紅燒魚的時候會遇到的問題,所以我們一一來...
    創(chuàng)業(yè)夢想巔峰閱讀 452評論 0 0