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学习的更多相关文章

  1. 转 iOS Core Animation 动画 入门学习(一)基础

    iOS Core Animation 动画 入门学习(一)基础 reference:https://developer.apple.com/library/ios/documentation/Coco ...

  2. 荼菜的iOS笔记--UIView的几个Block动画

    前言:我的第一篇文章荼菜的iOS笔记–Core Animation 核心动画算是比较详细讲了核心动画的用法,但是如你上篇看到的,有时我们只是想实现一些很小的动画,这时再用coreAnimation就会 ...

  3. iOS UIView动画效果 学习笔记

    //启动页动画 UIImageView *launchScreen = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds]; ...

  4. ios -- 教你如何轻松学习Swift语法(一)

    目前随着公司开发模式的变更,swift也显得越发重要,相对来说,swift语言更加简洁,严谨.但对于我来说,感觉swift细节的处理很繁琐,可能是还没适应的缘故吧.基本每写一句代码,都要对变量的数据类 ...

  5. iOS 视频全屏功能 学习

    项目中,也写过类似"视频全屏"的功能, 前一阵子读到今日头条 的一篇技术文章,详细介绍三种旋转方法差异优劣最终择取.文章从技术角度看写的非常好,从用户角度看,也用过多家有视频功能的 ...

  6. iOS 事件响应者链的学习(也有叫 UI连锁链)

    当发生事件响应的时候,必须知道由谁来响应事件.在iOS中,由响应链来对事件进行响应,所有的事件响应的类都是继承于UIResponder的子类,响应链是一个由不同对象组成的层次结构,其中每个对象将依次获 ...

  7. iOS开发UIView.h简介

    1.UICoordinateSpace不同坐标空间的坐标切换 @protocol UICoordinateSpace <NSObject> //将当前的坐标空间点转换到指定的坐标空间 - ...

  8. UIView Animation 动画学习总结

    目录 一.前言 二.UIView Animation 2.1 简单动画 2.2 关键帧动画 2.3 View 的转换 三.CALayer Animation 3.1 基本动画(CABasicAnima ...

  9. iOS 自定义方法 - UIView扩展

    示例代码 //#import <UIKit/UIKit.h>@interface UIView (LPCView)/** 上 */@property CGFloat top;/** 下 * ...

随机推荐

  1. OSI(Open System Interconnection)网络模型

    OSI模型是国际互连网标准化组织(International Standards Organizations ISO)所定义的,为了使网络的各个层次有标准.这个模型一般被称为“ISO OSI(Open ...

  2. (笔记)angular 路由

  3. RequireJS首次加载偶尔失败

    现象:第一次加载JS文件,首次加载偶尔失败: 原因:require(['jquery', 'operamasks', 'zTree', 'jQueryCookie'],中前后引用同步加载: 解决方式: ...

  4. windows服务访问网络资源(局域网内共享的文件夹)

    参考: 1.http://www.cnblogs.com/jak-black/articles/windows.html 2.http://q.cnblogs.com/q/25391/ 网络映射 1. ...

  5. su:认证失败

    使用命令[su - root]切换用户,提示[su:认证失败] 原因:Ubuntu安装之后,root用户默认是被锁定的,不允许登录,也不允许su到root. 解决:重新设置密码 在终端输入命令:sud ...

  6. sqlite3里类似top的用法

    sqlite3里类似top的用法 在sqlserver中使用top是很正常的,类似这样的语句: SELECT TOP 10 * FROM [index] ORDER BY id DESC; 但是很不幸 ...

  7. 比特币钱包应用breadwallet源码

    breadwallet是一款安全.可靠和便捷的比特币钱包,可使用户免于恶意软件和其他应用中常见的安全问题的骚扰,充分利用了iOS提供的安全功能,包括AES硬件加密.app沙盒和数据保护.代码签名以及k ...

  8. u-boot-1.1.6移植之dm9000

    网卡dm9000的执行过程(u-boot版本:u-boot-1.1.6): 在board.c里面有eth_initialize(gd->bd); eth_initialize的实现在eth.c里 ...

  9. POJ C++程序设计 编程题#3 编程作业—多态与虚函数

    编程题 #3 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面的程序输出 ...

  10. 微软ASP.NET MVC 学习地址

    微软ASP.NET MVC4.0学习地址:http://www.asp.net/mvc