ios基于UITabBarController实现tab页面切换

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

ios基于UITabBarController实现tab页面切换_第1张图片

具体实现代码如下:

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


你可能感兴趣的:(IOS入门)