在AppDelegate.m中写
#import "AppDelegate.h"
#import "RootViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FouthViewController.h"
#import "FifthViewController.h"
#import "SixViewController.h"
@interface AppDelegate ()<UITabBarControllerDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor=[UIColor whiteColor];
[self.window makeKeyAndVisible];
[self.window release];
RootViewController *rootVC=[[RootViewController alloc] init];
UINavigationController *naVC=[[UINavigationController alloc] initWithRootViewController:rootVC];
//创建一个tabbar的按钮内容
naVC.tabBarItem=[[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1000] autorelease];
//提示有几条消息
naVC.tabBarItem.badgeValue=@"23333";
//创建第二个naVC
SecondViewController *secVC=[[SecondViewController alloc] init];
UINavigationController *secNAVC=[[UINavigationController alloc] initWithRootViewController:secVC];
secNAVC.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"home" image:[UIImage imageNamed:@"1.jpg"] tag:1001];
//创建第三个naVC
ThirdViewController *thirdVC=[[ThirdViewController alloc] init];
UINavigationController *thirdNAVC=[[UINavigationController alloc] initWithRootViewController:thirdVC];
thirdNAVC.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"chat" image:[UIImage imageNamed:@"chat.jpg"] selectedImage:[UIImage imageNamed:@"f.jpg"]];
//创建第四个
FouthViewController *fouthVC=[[FouthViewController alloc] init];
UINavigationController *fouthNAVC=[[UINavigationController alloc] initWithRootViewController:fouthVC];
fouthNAVC.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"date" image:[UIImage imageNamed:@"date.png"] tag:1002];
//创建第五个
FifthViewController *fifthVC=[[FifthViewController alloc] init];
UINavigationController *fifthNAVC=[[UINavigationController alloc] initWithRootViewController:fifthVC];
fifthNAVC.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"type" image:[UIImage imageNamed:@"type.png"] tag:1003];
//创建第六个
SixViewController *sixVC=[[SixViewController alloc] init];
UINavigationController *sixNAVC=[[UINavigationController alloc] initWithRootViewController:sixVC];
sixNAVC.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"contact" image:[UIImage imageNamed:@"qq.png"] tag:1004];
//创建tabbarController
UITabBarController *tabVC=[[UITabBarController alloc] init];
//设置tabVC管理的naVC
tabVC.viewControllers=@[naVC,secNAVC,thirdNAVC,fouthNAVC,fifthNAVC,sixNAVC];
//设置window的根视图控制器
self.window.rootViewController=tabVC;
//设置一下tabbar的外观
//默认是透明的
tabVC.tabBar.translucent=NO;
// tabVC.tabBar.barTintColor=[UIColor yellowColor];
tabVC.tabBar.tintColor=[UIColor redColor];
//设置代理人
tabVC.delegate=self;
return YES;
}
这个方法相当于tab的点击方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
viewController.tabBarItem.badgeValue=nil;
}