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 ...
随机推荐
- DELL R730 服务器拷贝大文件
从服务器上拷贝大文件,通过USB拷贝,写入速度很慢,而且拷贝到100多G的时候直接卡死. 原因:服务器的USB是2.0,传输速率很慢. 解决方法: 找一台笔记本,USB 接口是3.0的,通过网络共享传 ...
- (转)编写高质量的OC代码
点标记语法 属性和幂等方法(多次调用和一次调用返回的结果相同)使用点标记语法访问,其他的情况使用方括号标记语法. 良好的风格: view.backgroundColor = [UIColor or ...
- LeetCode(125) Valid Palindrome
题目 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ign ...
- POJ 1273 Drainage Ditches(最大流Dinic 模板)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, ...
- python中set()函数的用法
set顾名思义是集合,里面不能包含重复的元素,接收一个list作为参数 list1=[1,2,3,4] s=set(list1) print(s) #逐个遍历 for i in s: print(i) ...
- PAT Basic 1060
1060 爱丁顿数 英国天文学家爱丁顿很喜欢骑车.据说他为了炫耀自己的骑车功力,还定义了一个“爱丁顿数” E ,即满足有 E 天骑车超过 E 英里的最大整数 E.据说爱丁顿自己的 E 等于87. 现给 ...
- 【03】github的markdown语法
[03]github的markdown语法 https://guides.github.com/features/mastering-markdown/(下图)(魔芋:已录入) http://ma ...
- appium+python自动化-微信公众号webview操作
前言 上一篇已经解决切换到微信公众号的webview上了,但是定位webview上元素的时候一直提示找不到,打印page_source也找不到页面上的元素,这个问题困扰了一整天,还好最后找到了原因, ...
- wordpress无法登录的解决方法
使用wordpress建站的朋友可能会遇到wordpress管理密码,有时甚至是正确的密码,但是多次尝试输入都无法登录,并且输入用户名和输入电子邮件都无法获取密码,遇到这种情况怎么办,本文教你如何处理 ...
- 【LeetCode】Combination Sum II(组合总和 II)
这道题是LeetCode里的第40道题. 题目要求: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. can ...