直接上代码:

//
// AppDelegate.m
//
// #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];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible]; [[UITabBar appearance] setBarTintColor:[UIColor lightGrayColor]];
[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]]; // RootViewController *rootVC = [[RootViewController alloc] init];
// UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:rootVC]; // 创建标签视图控制器
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];
/// 为当前创建的视图控制器指定标签,将要被标签控制器所管理的视图控制器须要为其提供 tabBarItem 对象才可在标签栏上显示相应的标签。
// 标签控制器管理的额视图控制器一旦数量超过五个,就会自己主动生成一个 more 标签相应的列表视图控制器管理多余的视图控制器,一般项目开发中有标签视图控制器管理的视图控制器也不会设计为超过五个的情况,大多数集中为 4 个视图控制器。 // aViewController.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:arc4random() % 12 tag:100 + i] autorelease];
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];
//当图片位置大小不合适时,能够使用 tabBarItem 的 imageInsets 属性来调整其大小和位置。
aViewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6 , -5, -6, 5); // aViewController.title = [NSString stringWithFormat:@"第%d个", i + 1];
// 显示 ① ② ③ ④ ⑤ 样式的
aViewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", i + 1];
[viewController addObject:aViewController];
}
tabBarController.viewControllers = viewController;
// // 设置 标签栏的 tintColor 和 barTintColor
// tabBarController.tabBar.tintColor = [UIColor greenColor];
// // 一些系统样式的标签高亮颜色会依赖标签栏的 tintColor 属性。
// tabBarController.tabBar.barTintColor = [UIColor redColor];
// 设置默认 选中的 标签下标
tabBarController.selectedIndex = 2;
self.previousIndex = tabBarController.selectedIndex;
//指定标签控制器的代理对象
tabBarController.delegate = self; self.window.rootViewController = tabBarController;
[tabBarController release]; // tabBarController.viewControllers = @[navi];
// self.window.rootViewController = tabBarController;
//
// [rootVC release];
// [navi release];
// [tabBarController release]; return YES;
} // 是否同意切换到选中的视图控制器
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSLog( @"%s", __FUNCTION__ ) ;
// 当点击标签切换视图控制器时标签控制器会通过此协议方法来确定是否同意切换到相应控制器。假设返回为NO,则不能切换视图控制器。
return YES;
} - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog( @"%s", __FUNCTION__ ) ;
// viewController.tabBarItem.badgeValue = nil;
if (self.previousIndex != tabBarController.selectedIndex) {
UIViewController *aViewController = tabBarController.viewControllers[self.previousIndex];
aViewController.tabBarItem.badgeValue = nil;
self.previousIndex = tabBarController.selectedIndex; }
} - (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:.
} @end // AppDelegate

UITabBarController ---- 标签视图控制器的更多相关文章

  1. UITabBarController — 标签视图控制器

    UITabBarController - 标签视图控制器 UITabBarController 分为三层结构: (1).tab bar (2.)Custom Content (3.). Tab bar ...

  2. 自定义UITabBarController标签视图控制器

    首先创建一个类,继承自UItabBarController 然后在.m文件中: 这里我有两个宏定义: #define WIDTH (myView.frame.size.width / 4) //我在写 ...

  3. 标签视图控制器UITabBarController

    标签视图控制器 UITabBarController FirstViewController*first = [[FirstViewController alloc] init]; //创建一个UIT ...

  4. [Xcode 实际操作]三、视图控制器-(2)UITabBarController选项卡(标签)视图控制器

    目录:[Swift]Xcode实际操作 本文将为你演示,选项卡视图控制器的创建和使用. 在项目文件夹[DemoApp]上点击鼠标右键,弹出右键菜单. [New File]->[Cocoa Tou ...

  5. iOS学习22之视图控制器

    1.自定义视图 1> 概述   定义视图:系统标准UI之外,自己组合而出的新的视图. 定义视图的优点: iOS提供了很多UI组件,借助它们我们可以实现不同的功能.尽管如此,实际开发中,我们还需要 ...

  6. 集合视图控制器(CollectionViewController) 、 标签控制器(TabBarController) 、 高级控件介绍

    1 创建集合视图,设置相关属性以满足要求 1.1 问题 集合视图控制器UIConllectionViewController是一个展示大量数据的控制器,系统默认管理着一个集合视图UICollectio ...

  7. Swift - 标签条(UITabBar)标签页控制器(UITabBarController)用法

    App底部的tab标签页可以方便的把功能模块划分清楚,只需点击相应的标签页就可以展示完全独立的视图页面,同时各标签页间的视图也可以进行数据交换.   TabBarItem系统自带图标样式(System ...

  8. 【iOS开发-30】UITabBarController的几种代理方法以及结合NSUserDefaults还原上次退出时被选中视图控制器和视图控制器的顺序

    一.UITabBarController的几种代理方法 在AppDelegate.h中加入一个协议<UITabBarControllerDelegate>.然后再AppDelegate.m ...

  9. 和iPhone有关的视图控制器:UIViewController、UITabBarController、UINavigationController及其混合用法

    iPhone中的view视图是应用程序对于数据最直观.最直接的呈现方式,如下是我在学习了iPhone中的视图控制器以及由其衍生的特殊子类的总结,希望对那些初学者有所帮助: UIViewControll ...

随机推荐

  1. LeetCode(136) Single Number

    题目 Given an array of integers, every element appears twice except for one. Find that single one. Not ...

  2. 20130829ios cocos2d下拉列表的向上弹出实现(ios开发遇到的frame的问题)

    前几天仔细区分了ios中frame,bounds,center之间的关系. Frame:边框矩形,是视图相对于其父坐标的位置和大小 Bounds:边界矩形,是本地坐标系统(一般较少使用) Center ...

  3. 使用Lucene的java api 写入和读取索引库

    import org.apache.commons.io.FileUtils;import org.apache.lucene.analysis.standard.StandardAnalyzer;i ...

  4. Spring核心技术(十二)——基于Java的容器配置(二)

    使用@Configuration注解 @Configuration注解是一个类级别的注解,表明该对象是用来指定Bean的定义的.@Configuration注解的类通过@Bean注解的方法来声明Bea ...

  5. vs系列自带的localdb数据库当做数据库

    我在学习mvc4框架的时候,因为是笔记本,觉得装了一个vs就很大了,再装一个sql server 就更麻烦,因此,就想到使用vs自带的localDB,直接生成预览的时候是没有问题的,但是当把mvc4的 ...

  6. iOS 引入支付宝 缺少 #include <openssl/asn1.h>

    出现情况如上图: 解决方法如下:在你的Xcode里的header search paths 里添加支付宝SDK(openssl的路径):格式如下  $(PROJECT_DIR)/文件夹名        ...

  7. mysql数据库修改字段类型

    修改字段类型: alter table 表名 modify column 字段名字 decimal(18, 4) ;

  8. Python unittest 学习

    import unittest class UTest(unittest.TestCase): def test_upper(self): self.assertEqual('foo'.upper() ...

  9. [SDOI2008]仪仗队 (欧拉函数)

    题目描述 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是否整齐(如下图 ...

  10. [SCOI2005]最大子矩阵 (动态规划)

    题目描述 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. 输入输出格式 输入格式: 第一行为n,m,k(1≤n≤100,1≤m≤2 ...