您的位置:首页 > 产品设计 > UI/UE

UITabbarViewController代码重构

2015-12-06 15:33 447 查看
原始代码:

UIViewController *vc1 = [[UIViewController alloc] init];
vc1.tabBarItem.title = @"首页";
vc1.tabBarItem.image = [UIImage imageNamed:@"tabbar_home"];
vc1.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_home_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//取消默认蓝色,按原始图片输出
[vc1.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[vc1.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateHighlighted];
vc1.view.backgroundColor = randomColor;

UIViewController *vc2 = [[UIViewController alloc] init];
vc2.tabBarItem.title = @"消息";
vc2.tabBarItem.image = [UIImage imageNamed:@"tabbar_message_center"];
vc2.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_message_center_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[vc2.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[vc2.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateHighlighted];
vc2.view.backgroundColor = randomColor;

UIViewController *vc3 = [[UIViewController alloc] init];
vc3.tabBarItem.title = @"发现";
vc3.tabBarItem.image = [UIImage imageNamed:@"tabbar_discover"];
vc3.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_discover_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[vc3.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[vc3.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateHighlighted];
vc3.view.backgroundColor = randomColor;

UIViewController *vc4 = [[UIViewController alloc] init];
vc4.tabBarItem.title = @"我";
vc4.tabBarItem.image = [UIImage imageNamed:@"tabbar_profile"];
vc4.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_profile_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[vc4.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[vc4.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateHighlighted];
vc4.view.backgroundColor = randomColor;

//tabBarVC.viewControllers = @[vc1,vc2];
[tabBarVC addChildViewController:vc1];
[tabBarVC addChildViewController:vc2];
[tabBarVC addChildViewController:vc3];
[tabBarVC addChildViewController:vc4];

重构之后

HomeViewController *homeVC = [[HomeViewController alloc] init];
[self addChildVC:homeVC title:NSLocalizedString(@"HomeTitle",@"HomeTitle") image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];

MessageCenterViewController *messageCenterVC = [[MessageCenterViewController alloc] init];
[self addChildVC:messageCenterVC title:NSLocalizedString(@"MessageCenterTitle", @"MessageCenterTitle") image:@"tabbar_message_center" selectedImage:@"tabbar_message_center"];

DiscoverViewController *discoverVC = [[DiscoverViewController alloc] init];
[self addChildVC:discoverVC title:NSLocalizedString(@"DiscoverTitle", @"DiscoverTitle") image:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];

ProfileViewController *profileVC = [[ProfileViewController alloc] init];
[self addChildVC:profileVC title:NSLocalizedString(@"ProfileTitle",@"ProfileTitle") image:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"];

/**
*  设置子控制器
*
*  @param childVC       子控制器
*  @param title         标题
*  @param image         图片
*  @param selectedImage 选中时的图片
*/
- (void)addChildVC:(UIViewController *)childVC title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{

NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName] = RGBACOLOR(123.0, 123.0, 123.0, 1.0);
NSMutableDictionary *selectedTextAttrs = [NSMutableDictionary dictionary];
selectedTextAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];

childVC.tabBarItem.title = title;
childVC.tabBarItem.image = [UIImage imageNamed:image];
childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[childVC.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[childVC.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateHighlighted];
childVC.view.backgroundColor = randomColor;
}

注意点:不要传类,要传控制器。因为如果传递类,那么创建就要封装在方法内部,但是初始化方法是可能发生变化的,因此,应该在需要的时候创建控制器,这样更佳灵活,易于扩展。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: