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. openSUSE之SSH登录

    环境: 1:操作系统:openSUSE 2:SSH工具:Xshell 3:virtualbox 解决问题:Xshell通过ssh登录openSUSE进行操作. 1:virtualbox安装好openS ...

  2. Android:控件布局(表格布局)TableLayout

    TableLayout继承LinearLayout 实例:用表格布局实现计算机布局>>>>>>>>>>>> 有多少个TableR ...

  3. Android IOS WebRTC 音视频开发总结(六一)-- 大数据解密国内实时通讯行业开发现状

    本文主要介绍国内实时通讯行业现状,文章最早发表在我们的微信公众号上,详见这里,欢迎关注微信公众号blackerteam,更多详见www.blackerteam.com 这几年移动互联网发展势头很猛,与 ...

  4. JSON对象遍历方法

    JSON对象提前不知道其属性和结构,遍历其值 var json2 = { "name": "txt1", "name2": "tx ...

  5. projecteuler Sum square difference

    The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square of ...

  6. [原]My first Python

    我的第一个Python程序: print 'hello world' raw_input ("print any key to continue...") 在python3.4下应 ...

  7. C#中常见的委托(Func委托、Action委托、Predicate委托)

    今天我要说的是C#中的三种委托方式:Func委托,Action委托,Predicate委托以及这三种委托的常见使用场景. Func,Action,Predicate全面解析 首先来说明Func委托,通 ...

  8. HUE 忘记密码

    解决方法: 启动HUE的Shell /opt/cloudera/parcels/CDH/lib/hue/build/env/bin/hue shell from django.contrib.auth ...

  9. Php output buffering缓存及程序缓存

       在php中有时为了控制程序的输出显示顺序,提供了output buffering缓存(php自身缓存机制). 若Ob缓存开启,需要输出的就先存在ob缓存里,再到程序缓存里.若没有开启,则直接进入 ...

  10. HTML5开发规范

    1.总体规范——采用html5的结构标签进行页面布局,注意结构的语义化,并注意页面大纲的层级结构.使用css3.0进行样式的设计. a.网页大纲查询网址http://gsnedders.html5.o ...