首页 > 代码库 > UI: 使用 UITabBarController 显示多视图控制器

UI: 使用 UITabBarController 显示多视图控制器

假设有两个视图控制器,它们的分别为 FirstViewController 和 SecondViewControlller。 现在到 app delegate 中定义视图控制器和标签栏。代码如下: 

 .h:

#import <UIKit/UIKit.h>#import "FirstViewController.h"#import "SecondViewController.h"@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (nonatomic ,strong) FirstViewController *firstViewController;@property (nonatomic ,strong) SecondViewController *secondViewController;@property (nonatomic ,strong) UITabBarController *tabBarControllser;@end

.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];    [self.window makeKeyAndVisible];    [self createViewControllers01];    return YES;}-(void)createViewControllers01{    //1    FirstViewController *firstVC = [[FirstViewController alloc]init];    UINavigationController *navfirst = [[UINavigationController alloc]initWithRootViewController:firstVC];    //2    SecondViewController *secondVC = [[SecondViewController alloc]init];    UINavigationController *navSecond = [[UINavigationController alloc]initWithRootViewController:secondVC];        //UINavigationController控制器数组    NSArray *arrayNav = [[NSArray alloc]initWithObjects:navfirst,navSecond, nil];/*************************/    UITabBarController *tbc = [[UITabBarController alloc]init];    tbc.viewControllers = arrayNav;    self.window.rootViewController = tbc;}

如果视图多的时候,另一种方法:

- (void)createViewControllers02{    //视图数组    NSArray *arrayVC = [NSArray arrayWithObjects:@"firstVC",@"secondVC", nil];    //每个视图对应的颜色数组    NSArray *colorVC = [NSArray arrayWithObjects:[UIColor redColor],[UIColor blackColor], nil];    //每个视图对应的tabbar图片数组    NSArray *tabBarImageTitle = [NSArray arrayWithObjects:@"tabbar_first",@"tabbar_second", nil];    //每个视图对用的tabbar标题数组    NSArray *tabbarTitle = [NSArray arrayWithObjects:@"",@"", nil];        NSMutableArray *arrayTBC = [[NSMutableArray alloc]init];    for (int i =0; i<arrayVC.count; i++) {        //将字符串转换为某个抽象类        Class cVC = NSClassFromString([arrayVC objectAtIndex:i]);        //初始化后赋值给视图控制器对象        UIViewController *vc = [[cVC alloc]init];        //给视图控制器添加背景颜色        vc.view.backgroundColor = [colorVC objectAtIndex:i];        //初始化每个视图对应的tabBarItem并将其赋值给每个视图        UITabBarItem *tabBarItem = [[UITabBarItem alloc]initWithTitle:[tabbarTitle objectAtIndex:i] image:[UIImage imageNamed:[tabBarImageTitle objectAtIndex:i]] tag:i+100];        vc.tabBarItem = tabBarItem;        //将每个视图加入视图控制器并存入数组        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];        [arrayTBC addObject:nav];    }    //初始化UITabBarController并对其赋值    UITabBarController *tbc = [[UITabBarController alloc]init];    tbc.viewControllers = arrayTBC;}

 

UI: 使用 UITabBarController 显示多视图控制器