IOS之UIView的tag学习
tag是UIView的一个属性,而且要求tag值唯一。父视图可以通过tag来找到一个子视图
UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(, , CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
redView.backgroundColor = [UIColor redColor];
redView.tag = ;
[self.window addSubview:redView]; UIView *getView = [self.window viewWithTag:];
tag用法
下面来看下几种需要注意的错误示例
由于示例代码有点多,怕麻烦的童鞋可以跳到最后的结论部分(最好把错误示例4的代码看一下)
一,view下有tag值相同的两个subview
UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(, , CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
redView.backgroundColor = [UIColor redColor];
redView.tag = ; UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(, CGRectGetHeight(self.window.frame)/, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
yellowView.backgroundColor = [UIColor yellowColor];
yellowView.tag = ; [self.window addSubview:yellowView];
[self.window addSubview:redView]; UIView *getView = [self.window viewWithTag:];
[getView setBackgroundColor:[UIColor whiteColor]];
错误示例1
结果是yellowView的背景被改变了,说明这种情况下会取得父视图加载的第一个tag是1000的子视图。
二,父视图取得子视图的子视图。
UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(, , CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
redView.backgroundColor = [UIColor redColor];
redView.tag = ; UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(, CGRectGetHeight(self.window.frame)/, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
yellowView.backgroundColor = [UIColor yellowColor];
yellowView.tag = ; UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(, , , )];
blueView.backgroundColor = [UIColor blueColor];
blueView.tag = ; UIView *greenView = [[UIView alloc]initWithFrame:CGRectMake(, , , )];
greenView.backgroundColor = [UIColor greenColor];
greenView.tag = ; [redView addSubview:blueView];
[yellowView addSubview:greenView]; [self.window addSubview:yellowView];
[self.window addSubview:redView]; UIView *getView = [self.window viewWithTag:];
[getView setBackgroundColor:[UIColor whiteColor]];
示例2
结果是greenView背景被改变了,说明这个是可行的。
三,取存在但不是自己子视图的视图
将示例2中的倒数第二句代码改为
UIView *getView = [redView viewWithTag:];
结果是greenView的背景没有被改变,说明这是行不通的。
四,别的视图中的子视图和本视图的子视图有相同的tag值
UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(, , CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
redView.backgroundColor = [UIColor redColor];
redView.tag = ; UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(, CGRectGetHeight(self.window.frame)/, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
yellowView.backgroundColor = [UIColor yellowColor];
yellowView.tag = ; UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(, , , )];
blueView.backgroundColor = [UIColor blueColor];
blueView.tag = ; UIView *greenView = [[UIView alloc]initWithFrame:CGRectMake(, , , )];
greenView.backgroundColor = [UIColor greenColor];
greenView.tag = ; [redView addSubview:blueView];
[yellowView addSubview:greenView]; [self.window addSubview:yellowView];
[self.window addSubview:redView]; UIView *getView = [redView viewWithTag:];
[getView setBackgroundColor:[UIColor whiteColor]];
错误示例3
结果来看还可以
五,greenView和redView的tag值相同
UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(, , CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
redView.backgroundColor = [UIColor redColor];
redView.tag = ; UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(, CGRectGetHeight(self.window.frame)/, CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)/)];
yellowView.backgroundColor = [UIColor yellowColor];
yellowView.tag = ; UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(, , , )];
blueView.backgroundColor = [UIColor blueColor];
blueView.tag = ; UIView *greenView = [[UIView alloc]initWithFrame:CGRectMake(, , , )];
greenView.backgroundColor = [UIColor greenColor];
greenView.tag = ; [redView addSubview:blueView];
[yellowView addSubview:greenView]; [self.window addSubview:yellowView];
[self.window addSubview:redView]; UIView *getView = [self.window viewWithTag:];
[getView setBackgroundColor:[UIColor whiteColor]];
错误示例4
结果是greenView的背景色被改变了。
最后说一下结论
由以上的代码可以推出,父视图通过tag取得子视图的顺序是深度优先,也就是先查看自己的第一个子视图,然后查看第一个子视图的所有子视图的
tag值,直到第一个子视图下的所有子视图搜索完,再搜索自己第二个子视图,直到找到为止。找不到就返回nil。
当然,最稳妥的方法还是确保tag值的唯一。
IOS之UIView的tag学习的更多相关文章
- 转 iOS Core Animation 动画 入门学习(一)基础
iOS Core Animation 动画 入门学习(一)基础 reference:https://developer.apple.com/library/ios/documentation/Coco ...
- 荼菜的iOS笔记--UIView的几个Block动画
前言:我的第一篇文章荼菜的iOS笔记–Core Animation 核心动画算是比较详细讲了核心动画的用法,但是如你上篇看到的,有时我们只是想实现一些很小的动画,这时再用coreAnimation就会 ...
- iOS UIView动画效果 学习笔记
//启动页动画 UIImageView *launchScreen = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds]; ...
- ios -- 教你如何轻松学习Swift语法(一)
目前随着公司开发模式的变更,swift也显得越发重要,相对来说,swift语言更加简洁,严谨.但对于我来说,感觉swift细节的处理很繁琐,可能是还没适应的缘故吧.基本每写一句代码,都要对变量的数据类 ...
- iOS 视频全屏功能 学习
项目中,也写过类似"视频全屏"的功能, 前一阵子读到今日头条 的一篇技术文章,详细介绍三种旋转方法差异优劣最终择取.文章从技术角度看写的非常好,从用户角度看,也用过多家有视频功能的 ...
- iOS 事件响应者链的学习(也有叫 UI连锁链)
当发生事件响应的时候,必须知道由谁来响应事件.在iOS中,由响应链来对事件进行响应,所有的事件响应的类都是继承于UIResponder的子类,响应链是一个由不同对象组成的层次结构,其中每个对象将依次获 ...
- iOS开发UIView.h简介
1.UICoordinateSpace不同坐标空间的坐标切换 @protocol UICoordinateSpace <NSObject> //将当前的坐标空间点转换到指定的坐标空间 - ...
- UIView Animation 动画学习总结
目录 一.前言 二.UIView Animation 2.1 简单动画 2.2 关键帧动画 2.3 View 的转换 三.CALayer Animation 3.1 基本动画(CABasicAnima ...
- iOS 自定义方法 - UIView扩展
示例代码 //#import <UIKit/UIKit.h>@interface UIView (LPCView)/** 上 */@property CGFloat top;/** 下 * ...
随机推荐
- 有没有好用的开源sql语法分析器? - 匿名用户的回答 - 知乎
有没有好用的开源sql语法分析器? - 匿名用户的回答 - 知乎 presto,hive,drill,calcite,sparksq
- C++记录debug信息的log类
取自:http://www.viksoe.dk/code/all_mfc.htm,里面有各种MFC常用的类 // LogFile.h: interface for the CLogFile class ...
- A planning attack on a commuter train carriage in Taipei
Last night an explosion on a commuter train carriage in Taipei Songshan railway station wounded at l ...
- 一分钟学会(一):.NET之正则表达式
本文介绍正则表达式在.NET中的基本应用,代码简单粗暴,实例浅显易懂,让你一分钟快速上手正则(大鸟请略过). 本文为入门文章,很多时候我们只是忘记了语法,这也可作为一个快速查询的参考. 如果想深入学习 ...
- Ubuntu点滴--apt-get update和upgrade的作用
update update is used to resynchronize the package index files from their sources. The indexes of av ...
- shell 截取指定的字符串
按指定的字符串截取 1.第一种方法: ${varible##*string} 从左向右截取最后一个string后的字符串 ${varible#*string}从左向右截取第一个string后的字符串 ...
- iOS 技术博客分享
1.了解有什么新技术 1> 苹果API文档 - General - Guides - iOSx API Diffs 2> 观看WWDC会议视频 2.如何使用新技术 1> 自己根据AP ...
- 单列模式(data与business交互)
public class CommentsBusiness //Business { #region 单列模式 private static CommentBusiness instance; pri ...
- js问题集锦
1.不在服务器中的访问,如file:///C:/Users/yx/Desktop/index.html这样的地址,ajax是无法访问的,不会执行send();必须放到服务器才可以. 2.阻止正常提交v ...
- php或js判断网站访问者来自手机或者pc
php或js判断网站访问者来自手机或者pc机 2013年9月26日,在弄wtuonline的时候为了区分用户是来自手机版浏览器还是pc,针对不同平台选择不同的网站版本,最终总结如下: ...