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 ...
随机推荐
- LeetCode(95) Unique Binary Search Trees II
题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...
- BZOJ 4557: [JLoi2016]侦察守卫
题目大意:每个点有一个放置守卫的代价,同时每个点放置守卫能覆盖到的距离都为d,问覆盖所有给定点的代价是多少. 题解: 树形DP f[x][y]表示x子树中所有点都已经覆盖完,并且x还能向上覆盖y层的最 ...
- jsonp实现跨域访问json数据
前台js function init() { $.ajax({ url: 'http://localhost:8012/index.json', dataType: "jsonp" ...
- 解决win7下pycharm移动文件出现Clear Read-Only status移动失败的问题
问题描述: 将pycharm中的文件move到指定文件夹或者将其他文件拖动到pycharm指定文件夹下,会出现如下问题导致文件移动失败: 出现这个问题的原因及解决办法如下: 第一种,pycharm下建 ...
- Python内置函数5
Python内置函数5 1.format参考前面字符串方法中的format 2.frozenset([iterable]) iterable -- 可迭代的对象,比如列表.字典.元组等等 返回一个冻结 ...
- wordpress需要FTP用户名密码的问题
wordpress安装删除插件需要FTP用户名密码的问题 方法一: 服务器命令操作: 1.在wordpress目录下面wp-config.php末尾加入下面代码: if(is_admin()) { ...
- [Kubernetes]Pod字段自动填充
PodPreset(Pod预设置)在Kubernetes v1.11以后出现,开发人员只需要提交一个基本的Pod YAML,Kubernetes就可以自动给对应的Pod对象加上运维人员设定好的其他必要 ...
- nginx的详解(四)
10.nginx的访问控制及DDOS预防1)访问控制配置基于各种原因,Ningx有时要进行访问控制.比如说,一般网站的后台都不能让外部访问,所以要添加 IP 限制,通常只允许公司的IP访问.访问控制就 ...
- RDDs基本操作、RDDs特性、KeyValue对RDDs、RDD依赖
摘要:RDD是Spark中极为重要的数据抽象,这里总结RDD的概念,基本操作Transformation(转换)与Action,RDDs的特性,KeyValue对RDDs的Transformation ...
- Linux命令——top
top命令可以实时动态地查看系统的整体运行情况,是一个综合了多方信息监测系统性能和运行信息的实用工具.通过top命令所提供的互动式界面,用热键可以管理. 语法 top(选项) 选项 -b:以批处理模式 ...