UITabBarController实现Tab切换详解手机开发

在很多的应用中,基本都是使用Tab方式进行模块间的切换。在Android中很多控件可以实现这类效果,TabLayout、TabHost…
而在iOS中系统为我们提供了UITabBarController类来实现Tab页面之间的切换。有几个页面我们就创建几个UINavigationController,然后通过tabbar.viewControllers = @[navi1, navi2, navi3…]; 设置给UITabBarController。在进行Tab切换的设计思想上Android和iOS是一致的,就是通过控件去管理页面,所以有几个Tab,我们就要创建几个View实例。

项目实例

这里写图片描述

首先创建3个页面:RedViewController.m、GreenViewController.m、
BlueViewController.m,三个页面的代码一样,只列举其一。
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  

创建UITabBarController对象,设置tabbar的子控制器,然后通过self.window.rootViewController = tabbar; 将tabbar赋值给根视图控制器。 代码如下:
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  

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/5947.html

(0)
上一篇 2021年7月17日
下一篇 2021年7月17日

相关推荐

发表回复

登录后才能评论