获取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];
}