获取TabbarController中点击item事件


获取TabbarController中点击item事件的方法

只需要在TabbarController里实现这个代理方法

    - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
        
        NSLog(@"%@",item.title);
    }

然后你可以通过字符串title的判断

就可以知道将要显示的是哪个控制器


tabbar属性设置

/// 设置 tabbar
    - (void)abountTabbar {
        
        //新闻
        XNNewsController *news = [XNNewsController newsControllerLoadXib];
        
        [self addChildViewController:news imageName:@"tabbar_home" title:@"新闻"];
        
        //本地
        XNHereController *ereCtrl = [XNHereController hereControllerLoadXib];
        
        [self addChildViewController:ereCtrl imageName:@"tabbar_message_center" title:@"本地"];
        
        // 爆料
        XNExplodeDataController *explodeCtrl = [XNExplodeDataController explodeDataControllerLoadXib];
        
        [self addChildViewController:explodeCtrl imageName:@"tabbar_discover" title:@"爆料"];
        
        // 我的
        XNMyViewController *myCtrl = [XNMyViewController myViewControllerLoadXib];
        
        [self addChildViewController:myCtrl imageName:@"tabbar_profile" title:@"我的"];
    }
    
    
    
    // 添加子控制器,并设置标题与图片
    - (void)addChildViewController:(UIViewController *)childCtrl imageName:(NSString *)imageName title:(NSString *)title{
        
        
        childCtrl.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        childCtrl.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        //设置标题
        childCtrl.tabBarItem.title = title;
        
        //指定一下属性
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        
        // 指定选中状态下文字颜色
        dic[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.835 green:0.090 blue:0.125 alpha:1.000];
        
        [childCtrl.tabBarItem setTitleTextAttributes:dic forState:UIControlStateSelected];
        
        [self addChildViewController:childCtrl];
        
        
    }

 

学自这个网址的回答:

 

 

转载于:https://my.oschina.net/codeismygirl/blog/631703

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值