iOS中的UITabBarController(标签视图控制器)

#import "AppDelegate.h"
#import "FirstTableViewController.h"
#import "SecondTableViewController.h"
#import "ThirdTableViewController.h"
#import "FourthTableViewController.h"
#import "FiveTableViewController.h"
#import "SixTableViewController.h"
@interface AppDelegate ()<UITabBarControllerDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    
    
    FirstTableViewController *firstVC = [[FirstTableViewController alloc] init];
    firstVC.tabBarItem.title = @"消息";
    firstVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_mainframe"];
    firstVC.tabBarItem.badgeValue = @"NEW";
    //设置选中之后的图片
    //firstVC.tabBarItem.selectedImage = [UIImage imageNamed:@"tabbar_contacts"];
    firstVC.tabBarItem.tag = 100;
    UINavigationController *firstNVC = [[UINavigationController alloc] initWithRootViewController:firstVC];
    
    SecondTableViewController *secondVC = [[SecondTableViewController alloc] init];
    secondVC.tabBarItem.title = @"通讯录";
    secondVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_contacts"];
    secondVC.tabBarItem.badgeValue = @"1";
    secondVC.tabBarItem.tag = 101;
    
    
    ThirdTableViewController *thirdVC = [[ThirdTableViewController alloc] init];
    thirdVC.tabBarItem.title = @"发现";
    thirdVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_discover"];
    thirdVC.tabBarItem.tag = 102;
    
    
    FourthTableViewController *fourthVC = [[FourthTableViewController alloc] init];
    fourthVC.tabBarItem.title = @"我的";
    fourthVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_me"];
    fourthVC.tabBarItem.tag = 103;
    
    FiveTableViewController *fiveVC = [[FiveTableViewController alloc] init];
    fiveVC.tabBarItem.title = @"下载";
    fiveVC.tabBarItem.image = [UIImage imageNamed:@"qr_toolbar_online_download_hl"];
    
    
    SixTableViewController *sixVC = [[SixTableViewController alloc] init];
    sixVC.tabBarItem.title = @"工具";
    sixVC.tabBarItem.image = [UIImage imageNamed:@"qr_toolbar_more_hl"];
    
    //创建标签视图控制器
    UITabBarController *tabBarVC = [[UITabBarController alloc] init];
    
    //1.配置标签控制器所管理的多个视图控制器
    NSArray *viewContrllers = @[firstNVC, secondVC, thirdVC, fourthVC,fiveVC, sixVC];
    
    tabBarVC.viewControllers = viewContrllers;
    
    //2.修改标签栏的颜色
    tabBarVC.tabBar.barTintColor = [UIColor whiteColor];
    //3.修改标签栏的渲染颜色
    tabBarVC.tabBar.tintColor = [UIColor greenColor];
    //4.设置默认选中的标签
    //tabBarVC.selectedIndex = 2;
    //设置默认选中的视图控制器
    tabBarVC.selectedViewController = thirdVC;
    
    //5.给tabBar设置图片
    //tabBarVC.tabBar.backgroundImage = [UIImage imageNamed:@"320x49"];
    
    tabBarVC.delegate=self;
    
    
    
    
    
    
    
    
    
    
    
    
    
    //将标签控制器指定为  window的根视图控制器
    self.window.rootViewController = tabBarVC;
    
    [firstVC release];
    [secondVC release];
    [thirdVC release];
    [fourthVC release];
    
    
    
    
    
    
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
#pragma mark
//询问标签是否可选中
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
   // NSLog(@"%s,%d",__FUNCTION__,__LINE__);
    return YES;
}
//当选中标签时触发
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    // NSLog(@"%s,%d",__FUNCTION__,__LINE__);
    if (101 == viewController.tabBarItem.tag) {
        viewController.tabBarItem.badgeValue = nil;
    }
}
//将要开始指定以标签视图控制器所管理的多个视图控制器时触发(点击 more中Edit按钮时触发)
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers {
    //NSLog(@"%s,%d",__FUNCTION__,__LINE__);
}
//完成标签栏的编辑时触发.
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{
     //NSLog(@"%s,%d",__FUNCTION__,__LINE__);
}







- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
-(void)dealloc{
    self.window = nil;
    [super dealloc];
}
@end
原文地址:https://www.cnblogs.com/wohaoxue/p/4811447.html