Assume there are two view controllers, firstviewcontroller and secondviewcontrolller. Now define the View Controller and label bar in APP delegate. The Code is as follows:
. 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;}
If there are multiple views, another method is as follows:
- (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: Use uitabbarcontroller to display multi-view Controllers