简单介绍UITabBarController使用

新建类从UITabBarController继承:


#import 

@interface TabViewController : UITabBarController

@end

#import "TabViewController.h"
#import "Item1ViewController.h"
#import "Item2ViewController.h"

@implementation TabViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        Item1ViewController *vc1 = [[Item1ViewController alloc]initWithNibName:@"Item1ViewController" bundle:nil];
        vc1.title = @"1";
        UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:vc1];
        
        Item2ViewController *vc2 = [[Item2ViewController alloc]initWithNibName:@"Item2ViewController" bundle:nil];
        vc2.title = @"2";
        UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:vc2];
        
        UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"One1" image:nil tag:0];
        nav1.tabBarItem = item1;
        
        UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"One2" image:nil tag:0];
        nav2.tabBarItem = item2;
        
        self.viewControllers = [NSArray arrayWithObjects:nav1,nav2, nil];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end

此处需要注意的时如果TabBarViewController的viewControllers们如果不包一层UINavigationController的话是无法显示导航栏的。






你可能感兴趣的:(IOS)