UITabBarController QQ

本文详细介绍了一个iOS应用从启动到展示主界面的过程,重点介绍了如何使用AppDelegate进行应用配置,创建并设置TabBarController作为根视图控制器,以及如何为各子ViewController设置tabBarItem。同时,文章还展示了如何在FirstViewController中添加统一的左按钮。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

转载于:https://www.cnblogs.com/zhizunbao/p/5414665.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值