UITabBarDelegate & UITabBarControllerDelegate详解

今天在做项目的时候遇到了一个问题:怎么判断进入一个(view)界面是tabbar左右切换显现的还由详情界面推出显示的,因为不同的显示view的方式,我要进行不同的处理。所以,就研究了一下UITabBarDelegate & UITabBarControllerDelegate

UITabBarDelegate

TabBarDelegate 一共有五个代理方法,而且代理方法必须写在UITabBarController的控制器里面,不然代理方法不会执行。直接在UITabBarController控制器里面写<UITabBarDelegate>,他就可以执行代理方法

代理方法

[目前感觉就是第一个有用,其他的不知道干啥]
- (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(@"已经开始自定制item");
}
- (void)tabBar:(UITabBar *)tabBar willEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed {
    NSLog(@"将要结束自定制item");
}
- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed{
    NSLog(@"将要结束自定制item");
}

UITabBarControllerDelegate

//设置控制器数组
@property(nullable, nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;
//设置控制器数组 动画
- (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;

    //创建相应的子控制器
    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委托内容

1、视图将要切换时调用,viewController为将要显示的控制器,如果返回的值为NO,则无法点击其它分栏了(viewController指代将要显示的控制器)
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{

   NSLog(@"被选中的控制器将要显示的按钮");
   //return NO;不能显示选中的控制器
   return YES;

}
 2、视图已经切换后调用,viewController 是已经显示的控制器

 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
  NSLog(@"视图显示后调用");
}
 3、将要开始自定义item的顺序

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

      NSLog(@"将要开始自定义item时调用");

      NSLog(@"%@",viewControllers);
}
 4、将要结束自定义item的顺序

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

{

       NSLog(@"将要结束自定义item时调用");

       NSLog(@"%@",viewControllers);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,194评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,090评论 25 709
  • “上午的工作时间到了,大家可以休息了,感觉时间真的过得好快啊。”主编分明温和却因室内的安静显得洪亮的声音快速穿过被...
    菠萝啦阅读 1,826评论 0 0
  • 古代与现代的社交异同 人只要是社会的人,只要活在这个社会上一天,就总会有各种各样的社交。但是,有一些社交是根本没...
    耐心的猎人阅读 3,596评论 0 0
  • 怎么做红烧鱼?油炸?煎?鱼皮要掉?有鱼腥味?味道不香?这些都是大家在做家常红烧鱼的时候会遇到的问题,所以我们一一来...
    创业梦想巅峰阅读 3,188评论 0 0