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:app

- (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;
}

若是視圖多的時候,另外一種方法:ide

- (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;
}
相關文章
相關標籤/搜索