AppDelegate.h 头文件
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UITabBarController * tabBar;
}
@property(nonatomic,retain)UITabBarController * tabBar;
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThreeViewController.h"
#import "FourViewController.h"
#import "FiveViewController.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize tabBar;//tabBar接口
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor]; //设置背景颜色
//===============================================
FirstViewController * first = [[[FirstViewController alloc]init]autorelease]; //实例化视图控制器
SecondViewController * second = [[[SecondViewController alloc]init]autorelease];
ThreeViewController * three = [[[ThreeViewController alloc]init]autorelease];
FourViewController * four = [[[FourViewController alloc]init]autorelease];
FiveViewController * five = [[[FiveViewController alloc]init]autorelease];
UINavigationController *firstNavController =[[UINavigationController alloc] initWithRootViewController:first]; //实例化导航控制器,并关联视图控制器
UINavigationController *secondNavController =[[UINavigationController alloc] initWithRootViewController:second];
UINavigationController *threeNavController = [[UINavigationController alloc] initWithRootViewController:three];
UINavigationController *fourNavController = [[UINavigationController alloc] initWithRootViewController:four];
UINavigationController *fiveNacController = [[UINavigationController alloc] initWithRootViewController:five];
NSArray * viewControllers = [NSArray arrayWithObjects:firstNavController,secondNavController,threeNavController,fourNavController,fiveNacController,nil]; //创建一个静态数组,并关联导航控制器
tabBar = [[UITabBarController alloc]init]; //为tabBar分配空间,并初始化
tabBar.viewControllers = viewControllers; //将导航控制器数组关联到tabBar
[self.window addSubview:tabBar.view]; //添加tabBar视图到window
//=================================================
[self.window makeKeyAndVisible];
return YES;
}
@end
一个视图控制器FirstViewController.h
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
{
//....................
}
@end
在FirstViewController.m中对tabBar进行设置
FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController()
@end
@implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.tabBarItem.image = [UIImage imageNamed:@"tabBar底部小图标.png"];
self.tabBarItem.title = @"小图标底部小字";
self.navigationItem.title = @"导航栏标题";
}
return self;
}