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; }
注意点:不要传类,要传控制器。因为如果传递类,那么创建就要封装在方法内部,但是初始化方法是可能发生变化的,因此,应该在需要的时候创建控制器,这样更佳灵活,易于扩展。
相关文章推荐
- android.util.AndroidRuntimeException: requestFeature() must be called before adding content
- ignoreAnchorPointForPosition(true) == 锚点(0,0)
- “简单证明GUID(全局唯一标识符)并不唯一”
- IOS学习之 UITextField设置内边距
- UEFI启动视频详解:启动分析+N项操作实例
- Resize a UIImage the right way
- Image Resizing Techniques_ iOS图像分辨率
- New UI-为TextView设置超链接跳转的三个方法
- iOS中创建UITableViewCell的正确姿态
- 【转】Qt中Ui名字空间以及setupUi函数的原理和实现
- 如何制作支持Legacy BIOS和UEFI BIOS两种模式启动的Windows PE
- 对于UIL(Universal Image Loader)的配置各项参数的不完全简介.
- uefi启动解析:由原理到实例
- UEFI+GPT、Legacy+MBR引导模式介绍 & 引导修复
- Android中的PID,TID和UID
- UEFI+GPT引导实践篇(二):UEFI引导安装64位Win7/Win8
- Machine Learning week 10 quiz: Large Scale Machine Learning
- 创建一个简单的quicjstart maven项目
- EasyUI - tab动态加载datagrid
- 总结使人进步,可视化界面GUI应用开发总结:Android、iOS、Web、Swing、Windows开发等