今天在做項目的時候遇到了一個問題:怎么判斷進入一個(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);
}