随便新建一个工程,然后在控制器中粘贴如下代码

- (void)viewDidLoad {

[super viewDidLoad];

UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];

view.backgroundColor = [UIColor greenColor];

NSArray * arr = [[NSArray alloc]initWithObjects:view, view, nil];

NSLog(@"====-----%@",arr);

UIView * view1 = arr[0];

view1.frame = CGRectMake(0, 100, 100, 100);

[self.view addSubview:view1];

UIView * view2 = arr[1];

view2.frame = CGRectMake(0, 100, 20, 20);

[self.view addSubview:view2];

UIView * view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];

view3.backgroundColor = [UIColor blueColor];

[self.view addSubview:view3];

UIView * view4 = [[UIView alloc]initWithFrame:CGRectMake(0, 300, 200, 200)];

view4.backgroundColor = [UIColor redColor];

[self.view addSubview:view4];

//    [view3 addSubview:view1];

//    [view3 addSubview:view2];

//一个view 只能加到最后一个 父控件上

[view4 addSubview:view2];

[view3 addSubview:view1];

}

运行后 的打印如下

====-----(

    "<UIView: 0x7fc8cdd01760; frame = (0 0; 100 100); layer = <CALayer: 0x600001cacfa0>>",

    "<UIView: 0x7fc8cdd01760; frame = (0 0; 100 100); layer = <CALayer: 0x600001cacfa0>>"

)

 

也就是说 虽然 数组中有两个元素  但是两个元素的地址 全都指向了同一块内存  也就是  view所在的内存。

简单理解为 数组存了 这个view对象的 指针, 通过  arr[0] 和 arr[1]拿到的都是同一个view。 

所以最后 加到view3 上一个view  加到view4上一个view   加的都是同一个。 而同一个view只能加到一个superview上 ,之前加的自动失效  或者可以理解为 自动被remove后 然后  加到新的 superView上。所以运行模拟器 就有了 一下的现象

关于数组中加入相同的view的试验的更多相关文章

  1. Vue 改变数组中对象的属性不重新渲染View的解决方案

    Vue 改变数组中对象的属性不重新渲染View的解决方案 在解决问题之前,我们先来了解下 vue响应性原理: Vue最显著的一个功能是响应系统-- 模型只是一个普通对象,修改对象则会更新视图.受到ja ...

  2. Android中自定义样式与View的构造函数中的第三个参数defStyle的意义

    零.序 一.自定义Style 二.在XML中为属性声明属性值 1. 在layout中定义属性 2. 设置Style 3. 通过Theme指定 三.在运行时获取属性值 1. View的第三个构造函数的第 ...

  3. 解析plist文件(字典里包着数组,数组中又包含字典)

    #import "RootTableViewController.h" #import "City.h" @interface RootTableViewCon ...

  4. php从数组中取出一段 之 array_slice

    array array_slice ( array $array , int $offset [, int $length [, bool $preserve_keys ]] ) array_slic ...

  5. js从数组中随机取出不同的元素

    前言 上午处理个需求需要从一个总数组中随机取出不同的元素.共使用两个方法.第一种方法较常规,经测试有bug,数据量大以后随机几次返回的对象直接是function而不是object. 当然简单数据类型应 ...

  6. [LeetCode] Find All Numbers Disappeared in an Array 找出数组中所有消失的数字

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  7. [LeetCode] Find All Duplicates in an Array 找出数组中所有重复项

    Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...

  8. [LeetCode] Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  9. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

随机推荐

  1. BZOJ 3171 [Tjoi2013]循环格(费用流)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3171 [题目大意] 一个循环格就是一个矩阵,其中所有元素为箭头,指向相邻四个格子. 每 ...

  2. 【动态规划】【单调队列】tyvj1305 最大子序和

    http://blog.csdn.net/oiljt12138/article/details/51174560 单调队列优化dp #include<cstdio> #include< ...

  3. python基础之文件操作,函数

    文件操作 +模式: r+t  w+t  a+t  r+b   w+b  a+b 可读可写,其他功能与人,r,w,a相同 f.seek(offset,whence) 文件指针移动    offest的单 ...

  4. 数据库之mysql的基本操作

    1 数据库 1.1 数据库介绍 为什么要有数据库? 数据需要大量的存储,把数据永久保存下来,可以保存到文件中,当然也可以保存到数据库中. 数据库:通俗的说,存储数据的仓库,是管理数据的文件夹:存放数据 ...

  5. JS零基础一步一步做应用全记录

    1.起因 作为几个外卖重度依赖癌晚期患者,呆宿舍的时候几个人一起叫外卖已经是常事.偶然看到隔壁宿舍在饿了么订餐的时候,看到在饿了么的首页上有一个谁去拿外卖的一个小游戏/工具,感觉这个小细节,饿了么把握 ...

  6. Web安全开发指南--会话管理

    1.会话管理 3.1.会话管理安全规则 1 避免在URL携带session id. 2 使用SSL加密通道来传输cookie. 3 避免在错误信息和调试日志中记录session id. 4 使用框架自 ...

  7. Inno Setup自定义卸载文件名称的脚本

    Inno Setup 支持在同一个目录中安装多个应用程序,所以根据安装的先后次序自动将卸载程序文件命名为 unins000.exe,unins001.exe,unins002.exe 等等.这是 IN ...

  8. linux安装Node(Centos)

    步骤 1.打开node官网,找到node版本的下载地址 这里我找到的地址是: https://npm.taobao.org/mirrors/node/v8.2.1/node-v8.2.1-linux- ...

  9. 在 Android Studio 2.2 中愉快地使用 C/C++

    转载请注明出处:http://blog.csdn.net/wl9739/article/details/52607010 注:官网上面的技术文章也在不断地汉化中,只是进度有点慢.在我翻译本篇文章的时候 ...

  10. LINUX之文件操作权限讲解

    r(Read,读取):对文件而言,具有读取文件内容的权限:对目录来说,具有浏览目 录的权限. w(Write,写入):对文件而言,具有新增.修改文件内容的权限:对目录来说,具有删除.移动目录内文件的权 ...