AppDelegate.m
1 #import "AppDelegate.h" 2 #import "FirstViewController.h" 3 #import "SecondViewController.h" 4 #import "ThirdViewController.h" 5 6 7 @interface AppDelegate () 8 9 @end 10 11 @implementation AppDelegate 12 13 14 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 15 // Override point for customization after application launch. 16 /** 17 * 第一步:创建window对象 18 */ 19 20 // 创建window 21 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 22 self.window.backgroundColor = [UIColor whiteColor]; 23 [self.window makeKeyAndVisible]; 24 25 26 27 /** 28 * 第二步:设置window的根视图控制器 29 */ 30 // 2.1 创建UINavigationController控制器,并指定导航控制器的根视图控制器 31 32 // 创建UITabBarController对象 33 UITabBarController *mainTab = [[UITabBarController alloc] init]; 34 35 // 将mainTab作为导航控制器的根视图控制器 36 UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:mainTab]; 37 38 39 // 设置控制器数组 40 41 FirstViewController *firstVC = [[FirstViewController alloc] init]; 42 firstVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:100]; 43 44 SecondViewController *secondVC = [[SecondViewController alloc] init]; 45 secondVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:101]; 46 47 ThirdViewController *thirdVC = [[ThirdViewController alloc] init]; 48 thirdVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:102]; 49 50 51 // 添加到控制器数组中 52 mainTab.viewControllers = @[firstVC, secondVC, thirdVC]; 53 54 55 56 // 设置window的根控制器 57 self.window.rootViewController = rootNav; 58 59 60 // 一键设置 61 [UINavigationBar appearance].barTintColor = [UIColor magentaColor]; 62 63 [UINavigationBar appearance].tintColor = [UIColor whiteColor]; 64 65 66 [UITabBar appearance].tintColor = [UIColor purpleColor]; 67 68 69 return YES; 70 } 71 72 73 @end
FirstViewController.m
1 #import "FirstViewController.h" 2 3 @interface FirstViewController () 4 5 @end 6 7 @implementation FirstViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 // Do any additional setup after loading the view. 12 13 // self.title = @"第一页"; 14 15 // self.tabBarController.navigationItem.title = @"第一页"; // 只走一次 16 17 NSLog(@"第一页已加载"); 18 19 20 // 在这里写,翻页的话,不会改变,永远留在这个位置,也就是添加统一的左按钮 21 self.tabBarController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(click:)]; 22 } 23 24 25 - (void)viewDidAppear:(BOOL)animated { 26 27 NSLog(@"第一页出现"); 28 29 self.tabBarController.navigationItem.title = @"第一页"; // 每次点击这一页,都会加载一次 30 } 31 32 33 - (void)click:(UIBarButtonItem *)sender { 34 35 NSLog(@"统一的左按钮被点击"); 36 } 37 38 @end