ios基于UITabBarController实现tab页面切换

本文介绍了如何在iOS中使用UITabBarController实现底部标签页的切换,通过示例代码展示了AppDelegate.m、RedViewController.m、GreenViewController.m和BlueViewController.m的配置过程。

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

首先,带大家来看看实现的效果图,点击底部三个tab按钮来实现页面的切换


具体实现代码如下:

AppDelegate.m

#import "AppDelegate.h"
#import "RedViewController.h"
#import "GreenViewController.h"
#import "BlueViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    //创建tabbar所管理的子控制器,每个子控制器都带有一个导航
    RedViewController *redVC = [[RedViewController alloc]initWithNibName:@"RedViewController" bundle:nil];
    UINavigationController *navi1 = [[UINavigationController alloc]initWithRootViewController:redVC];
    GreenViewController *greenVC = [[GreenViewController alloc]initWithNibName:@"GreenViewController" bundle:nil];
    UINavigationController *navi2 = [[UINavigationController alloc]initWithRootViewController:greenVC];
    BlueViewController *blueVC = [[BlueViewController alloc]initWithNibName:@"BlueViewController" bundle:nil];
    UINavigationController *navi3 = [[UINavigationController alloc]initWithRootViewController:blueVC];
    //创建UITabBarController对象
    UITabBarController *tabbar = [[UITabBarController alloc]init];
    //设置tabbar的子控制器
    tabbar.viewControllers = @[navi1, navi2, navi3];
    //将标签控制器设置为根视图控制器,程序的第一个界面默认为标签控制器所管理的第一个子控制器的视图
    self.window.rootViewController = tabbar;
    [self.window makeKeyAndVisible];
    
    // Override point for customization after application launch.
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end


RedViewController.m

#import "RedViewController.h"

@interface RedViewController ()

@end

@implementation RedViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"红色";
        self.tabBarItem.title = @"首页";
        self.tabBarItem.image = [UIImage imageNamed:@"menu_a"];
        self.tabBarItem.selectedImage = [UIImage imageNamed:@"menu_a1"];

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}



@end


GreenViewController.m

#import "GreenViewController.h"

@interface GreenViewController ()

@end

@implementation GreenViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"绿色";
        self.tabBarItem.title = @"发现";
        self.tabBarItem.image = [UIImage imageNamed:@"menu_b"];
        self.tabBarItem.selectedImage = [UIImage imageNamed:@"menu_b1"];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


@end


BlueViewController.m

#import "BlueViewController.h"

@interface BlueViewController ()

@end

@implementation BlueViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"蓝色";
        self.tabBarItem.title = @"我的";
        self.tabBarItem.image = [UIImage imageNamed:@"menu_d"];
        self.tabBarItem.selectedImage = [UIImage imageNamed:@"menu_d1"];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}



@end


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值