tgt1.首先,新建一个继承UITabBarController的类MainTabBarController(ps:名字可修改)。右键NewFile->cocoa touch class->next
2.新建TabBarController 的ViewControllers(这里新建两个继承与UIViewController的类,这里分别取名为FirstViewController和SecondViewController)
新建类的过程跟上边一样。
3.在MainViewController.m文件中实现如下代码
#import "MainViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addSubViewController];
// Do any additional setup after loading the view.
}
-(void)addSubViewController{
FirstViewController *FVC = [[FisrtViewController alloc]init];
UINavigationController *FNC = [[UINavigationController alloc]initWithRootViewController:FVC];
//初始化tabbaritem的标题和图标
UITabBarItem *FTB = [[UITabBarItem alloc]initWithTitle:@"activity" image:[UIImage imageNamed: @"activity.png"] tag:0];
saNC.tabBarItem = FTB;
SecondViewController *SVC = [[SecondViewController alloc]init];
UINavigationController *SNC = [[UINavigationController alloc]initWithRootViewController:SVC];
UITabBarItem *STB = [[UITabBarItem alloc] initWithTitle:@"help" image:[UIImage imageNamed:@"help.png"] tag:1];
SNC.tabBarItem=STB;
NSArray *controllerArray = @[FNC,SNC];
[self setViewControllers:controllerArray animated:YES];
}
4.在AppDelegate.m文件中的第一个方法,添加如下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
MainViewController *mainvc = [[MainViewController alloc]init];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = mainvc;
return YES;
}
5.在Target->general中改变Main Interface,将Main改为LaunchScreen(点击工程文件可进入):
6.运行效果如下: