Ui: Use uitabbarcontroller to display multi-view Controllers

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.