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. chrome浏览器设置小于12号的字体不起作用?

    在某些chrome浏览器下,css里设置的10号字体竟然不起作用!仍显示12号大小,对比firefox.ie6.7.8.9,他们的显示都是好的. 要是你也碰到这问题,可以这样解决: -webkit-t ...

  2. HTTP Status 500 - javax.servlet.ServletException

    运行某个jsp页面时提示 type Exception report message javax.servlet.ServletException: java.lang.NoClassDefFound ...

  3. JS 和 CSS 的位置对其他资源加载顺序的影响

    JS 和 CSS 在页面中的位置,会影响其他资源(指 img 等非 js 和 css 资源)的加载顺序,究其原因,有三个值得注意的点: JS 有可能会修改 DOM. 典型的,可能会有 document ...

  4. Windows USN Journal Parsing

    What is "USN Journal"? It is "Update Sequence Number Journal". It records change ...

  5. Android IOS WebRTC 音视频开发总结(二二)-- 多人视频架构模式

    本文主要介绍多人视频会议服务端架构方式,文章来自博客园RTC.Blacker,转载必须说明出处,欢迎关注个人微信公众号blacker,更多详见www.rtc.help 随着移动互联网的迅速发展,很多公 ...

  6. 逻辑回归的分布式实现 [Logistic Regression / Machine Learning / Spark ]

    1- 问题提出 2- 逻辑回归 3- 理论推导 4- Python/Spark实现 # -*- coding: utf-8 -*- from pyspark import SparkContext f ...

  7. .NET Web开发总结

    在aspx文件中  创建控件 在右下角有控件信息 按类排序 会将控件信息安装类排序 点击控件 会增加属性页面的分页[事件]页面  可以增加其事件函数 字符串操作及其时间操作 fn_name.Inser ...

  8. 遇到Wampserver遇到的问题

  9. Asp.net页面跳转Session丢失问题

    原本去年在做项目时,写好的一记篇博客分享给大家. Asp.net页面跳转Session丢失问题   编写人:CC阿爸 2014-4-2 l  近来在做泛微OA与公司自行开发的系统集成登录的问题.在使用 ...

  10. 关于PYTHON_EGG_CACHE无权限的问题

    Perhaps your account does not have write access to this directory? You can change the cache director ...