AppDelegate.m
#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
/**
* 第一步:创建window对象
*/ // 创建window
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible]; /**
* 第二步:设置window的根视图控制器
*/
// 2.1 创建UINavigationController控制器,并指定导航控制器的根视图控制器 // 创建UITabBarController对象
UITabBarController *mainTab = [[UITabBarController alloc] init]; // 将mainTab作为导航控制器的根视图控制器
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:mainTab]; // 设置控制器数组 FirstViewController *firstVC = [[FirstViewController alloc] init];
firstVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:]; SecondViewController *secondVC = [[SecondViewController alloc] init];
secondVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:]; ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
thirdVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:]; // 添加到控制器数组中
mainTab.viewControllers = @[firstVC, secondVC, thirdVC]; // 设置window的根控制器
self.window.rootViewController = rootNav; // 一键设置
[UINavigationBar appearance].barTintColor = [UIColor magentaColor]; [UINavigationBar appearance].tintColor = [UIColor whiteColor]; [UITabBar appearance].tintColor = [UIColor purpleColor]; return YES;
} @end
FirstViewController.m
#import "FirstViewController.h" @interface FirstViewController () @end @implementation FirstViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view. // self.title = @"第一页"; // self.tabBarController.navigationItem.title = @"第一页"; // 只走一次 NSLog(@"第一页已加载"); // 在这里写,翻页的话,不会改变,永远留在这个位置,也就是添加统一的左按钮
self.tabBarController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(click:)];
} - (void)viewDidAppear:(BOOL)animated { NSLog(@"第一页出现"); self.tabBarController.navigationItem.title = @"第一页"; // 每次点击这一页,都会加载一次
} - (void)click:(UIBarButtonItem *)sender { NSLog(@"统一的左按钮被点击");
} @end