UITabBarController ---- 标签视图控制器
直接上代码:
//
// 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 ---- 标签视图控制器的更多相关文章
- UITabBarController — 标签视图控制器
UITabBarController - 标签视图控制器 UITabBarController 分为三层结构: (1).tab bar (2.)Custom Content (3.). Tab bar ...
- 自定义UITabBarController标签视图控制器
首先创建一个类,继承自UItabBarController 然后在.m文件中: 这里我有两个宏定义: #define WIDTH (myView.frame.size.width / 4) //我在写 ...
- 标签视图控制器UITabBarController
标签视图控制器 UITabBarController FirstViewController*first = [[FirstViewController alloc] init]; //创建一个UIT ...
- [Xcode 实际操作]三、视图控制器-(2)UITabBarController选项卡(标签)视图控制器
目录:[Swift]Xcode实际操作 本文将为你演示,选项卡视图控制器的创建和使用. 在项目文件夹[DemoApp]上点击鼠标右键,弹出右键菜单. [New File]->[Cocoa Tou ...
- iOS学习22之视图控制器
1.自定义视图 1> 概述 定义视图:系统标准UI之外,自己组合而出的新的视图. 定义视图的优点: iOS提供了很多UI组件,借助它们我们可以实现不同的功能.尽管如此,实际开发中,我们还需要 ...
- 集合视图控制器(CollectionViewController) 、 标签控制器(TabBarController) 、 高级控件介绍
1 创建集合视图,设置相关属性以满足要求 1.1 问题 集合视图控制器UIConllectionViewController是一个展示大量数据的控制器,系统默认管理着一个集合视图UICollectio ...
- Swift - 标签条(UITabBar)标签页控制器(UITabBarController)用法
App底部的tab标签页可以方便的把功能模块划分清楚,只需点击相应的标签页就可以展示完全独立的视图页面,同时各标签页间的视图也可以进行数据交换. TabBarItem系统自带图标样式(System ...
- 【iOS开发-30】UITabBarController的几种代理方法以及结合NSUserDefaults还原上次退出时被选中视图控制器和视图控制器的顺序
一.UITabBarController的几种代理方法 在AppDelegate.h中加入一个协议<UITabBarControllerDelegate>.然后再AppDelegate.m ...
- 和iPhone有关的视图控制器:UIViewController、UITabBarController、UINavigationController及其混合用法
iPhone中的view视图是应用程序对于数据最直观.最直接的呈现方式,如下是我在学习了iPhone中的视图控制器以及由其衍生的特殊子类的总结,希望对那些初学者有所帮助: UIViewControll ...
随机推荐
- shell-code-5-流程控制
××××××××××××××××××××IF-ELSE×××××××××××××××××××××××××××××× a=1b=2if [ $a == $b ]then echo a等于belif [ ...
- 在 shell中, 我們可用 $0, $1, $2, $3 ... 這樣的变量分別提取命令行中变量
代码: script_name parameter1 parameter2 parameter3 ...我們很容易就能猜出 $0 就是代表 shell script 名称(路径)本身,而 $1 就是其 ...
- Eclipse安装以及安装时遇到的问题解决办法
1, 首先要安装JDK(最好使用最新版本),注意区分32位于64位 2, 安装程序,双击打开安装即可 3, 安装包下载:http://developer.android.com/sdk/index.h ...
- 练习题,新建数据库anyun
一.新建数据库,数据库名为anyun mysql> create database anyun; Query OK, 1 row affected (0.00 sec) 二.查看数据库 mysq ...
- Scrapy 应用之爬取《盗墓笔记》
爬取<盗墓笔记>和爬取<宦海沉浮>原理一样,但是使用了两种不同的追踪链接的方式,<盗墓笔记>使用的是跟踪下一页链接,直至没有下一页为止,<宦海沉浮>则是 ...
- 九度oj 题目1091:棋盘游戏
题目描述: 有一个6*6的棋盘,每个棋盘上都有一个数值,现在又一个起始位置和终止位置,请找出一个从起始位置到终止位置代价最小的路径: 1.只能沿上下左右四个方向移动 2.总代价是没走一步的 ...
- 怎么创建SpringBoot项目
上述中讲到了怎么创建SpringBoot项目,那么现在就来介绍下SpringBoot配置文件的两种格式yml和properties 首先呢发上一份application.properties 在放上一 ...
- BZOJ 3925 [Zjoi2015]地震后的幻想乡 ——期望DP
我们只需要考虑$\sum F(x)P(x)$的和, $F(x)$表示第x大边的期望,$P(x)$表示最大为x的概率. 经过一番化简得到$ans=\frac{\sum T(x-1)}{m+1}$ 所以就 ...
- BZOJ 1086 [SCOI2005]王室联邦 ——DFS
手把手教你树分块系列. 只需要记录一个栈,如果等于B的情况就弹栈,令省会为当前节点. 然后把待分块的序列不断上传即可. 考虑到有可能弹出不是自身节点的子树节点,所以记录一下当前的栈底. DFS即可 # ...
- bzoj1853: [Scoi2010]幸运数字 dp+容斥原理
在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是“幸运号码”!但是这种“幸运号码”总是 ...