直接上代码:
#import "AppDelegate.h"
#import "RootViewController.h"
#import "FirstViewController.h"
#import "SecnodViewController.h"
#import "ThirdViewController.h"
@interface AppDelegate()<UITabBarControllerDelegate>
@property (nonatomic, assign) NSInteger previousIndex ;
@end
@implementation AppDelegate
- (void)dealloc {
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[[UITabBar appearance] setBarTintColor:[UIColor lightGrayColor]];
[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSArray *classNames = @[@"FirstViewController", @"SecnodViewController", @"ThirdViewController", @"ThirdViewController", @"ThirdViewController"];
NSMutableArray *viewController = [NSMutableArray array];
for (int i = 0; i < classNames.count; i++) {
UIViewController *aViewController = [[NSClassFromString(classNames[i]) alloc] init];
UIImage *image = [[UIImage imageNamed:[NSString stringWithFormat:@"icon0%d", i + 1]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"icon0%d_s", i + 1]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
aViewController.tabBarItem = [[[UITabBarItem alloc] initWithTitle:nil image:image selectedImage:selectedImage] autorelease];
aViewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6 , -5, -6, 5);
aViewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", i + 1];
[viewController addObject:aViewController];
}
tabBarController.viewControllers = viewController;
tabBarController.selectedIndex = 2;
self.previousIndex = tabBarController.selectedIndex;
tabBarController.delegate = self;
self.window.rootViewController = tabBarController;
[tabBarController release];
return YES;
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSLog( @"%s", __FUNCTION__ ) ;
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog( @"%s", __FUNCTION__ ) ;
if (self.previousIndex != tabBarController.selectedIndex) {
UIViewController *aViewController = tabBarController.viewControllers[self.previousIndex];
aViewController.tabBarItem.badgeValue = nil;
self.previousIndex = tabBarController.selectedIndex;
}
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
@end