- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 1.创建窗口 self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; // 2.设置窗口的根控制器 CYXTabBarController *tabBarVC = [[CYXTabBarController alloc]init]; self.window.rootViewController = tabBarVC; // 3.显示窗口 [self.window makeKeyAndVisible]; return YES;}
- (void)viewDidLoad { [super viewDidLoad]; // 1.添加第一个控制器 // 1.1 初始化 CYXOneViewController *oneVC = [[CYXOneViewController alloc]init]; // 1.2 把oneVC添加为UINavigationController的根控制器 UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:oneVC]; // 设置tabBar的标题 nav1.title = @"首页"; [nav1.navigationBar setBackgroundImage:[UIImage imageNamed:@"commentary_num_bg"] forBarMetrics:UIBarMetricsDefault]; // 设置tabBar的图标 nav1.tabBarItem.image = [UIImage imageNamed:@"tab_home_icon"]; // 设置navigationBar的标题 oneVC.navigationItem.title = @"首页"; // 设置背景色(这些操作可以交给每个单独子控制器去做) oneVC.view.backgroundColor = [UIColor whiteColor]; // 1.3 把UINavigationController交给UITabBarController管理 [self addChildViewController:nav1]; // 2.添加第2个控制器 CYXTwoViewController *twoVC = [[CYXTwoViewController alloc]init]; UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:twoVC]; nav2.title = @"技术"; nav2.tabBarItem.image = [UIImage imageNamed:@"js"]; twoVC.navigationItem.title = @"技术"; twoVC.view.backgroundColor = [UIColor blueColor]; [self addChildViewController:nav2]; // 3.添加第3个控制器 CYXThreeViewController *threeVC = [[CYXThreeViewController alloc]init]; UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:threeVC]; nav3.title = @"博文"; nav3.tabBarItem.image = [UIImage imageNamed:@"qw"]; threeVC.navigationItem.title = @"博文"; threeVC.view.backgroundColor = [UIColor yellowColor]; [self addChildViewController:nav3]; // 4.添加第4个控制器 CYXFourViewController *fourVC = [[CYXFourViewController alloc]init]; UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:fourVC]; nav4.title = @"我的江湖"; nav4.tabBarItem.image = [UIImage imageNamed:@"user"]; fourVC.navigationItem.title = @"我的江湖"; fourVC.view.backgroundColor = [UIColor grayColor]; [self addChildViewController:nav4];}
但你可能会忍不住吐槽了,这些全是冗余的垃圾代码,没有可读性,下面就来抽取一下代码吧
第四步,抽取重复代码
- (void)viewDidLoad { [super viewDidLoad]; [self setUpAllChildViewController];}/** * 添加所有子控制器方法 */- (void)setUpAllChildViewController{ // 1.添加第一个控制器 CYXOneViewController *oneVC = [[CYXOneViewController alloc]init]; [self setUpOneChildViewController:oneVC image:[UIImage imageNamed:@"tab_home_icon"] title:@"首页"]; // 2.添加第2个控制器 CYXTwoViewController *twoVC = [[CYXTwoViewController alloc]init]; [self setUpOneChildViewController:twoVC image:[UIImage imageNamed:@"js"] title:@"技术"]; // 3.添加第3个控制器 CYXThreeViewController *threeVC = [[CYXThreeViewController alloc]init]; [self setUpOneChildViewController:threeVC image:[UIImage imageNamed:@"qw"] title:@"博文"]; // 4.添加第4个控制器 CYXFourViewController *fourVC = [[CYXFourViewController alloc]init]; [self setUpOneChildViewController:fourVC image:[UIImage imageNamed:@"user"] title:@"我的江湖"];}/** * 添加一个子控制器的方法 */- (void)setUpOneChildViewController:(UIViewController *)viewController image:(UIImage *)image title:(NSString *)title{ UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:viewController]; navC.title = title; navC.tabBarItem.image = image; [navC.navigationBar setBackgroundImage:[UIImage imageNamed:@"commentary_num_bg"] forBarMetrics:UIBarMetricsDefault]; viewController.navigationItem.title = title; [self addChildViewController:navC];}
新闻热点
疑难解答