而在iOS中系统为我们提供了UITabBarController类来实现Tab页面之间的切换。有几个页面我们就创建几个UINavigationController,然后通过tabbar.viewControllers = @[navi1, navi2, navi3…]; 设置给UITabBarController。在进行Tab切换的设计思想上Android和iOS是一致的,就是通过控件去管理页面,所以有几个Tab,我们就要创建几个View实例。




#import "RedViewController.h"   
@interface RedViewController ()   
@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.   

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

#import "AppDelegate.h"   
#import "RedViewController.h"   
#import "GreenViewController.h"   
#import "BlueViewController.h"   
@interface AppDelegate ()   
@implementation AppDelegate   
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];   
    self.window.backgroundColor = [UIColor whiteColor];   
    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 *tabbar = [[UITabBarController alloc]init];   
    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:.   


上一篇 2021年7月17日
下一篇 2021年7月17日