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

- (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. 【枚举】【字符串哈希】Gym - 101164K - Cutting

    给你A B两个串,让你切B串两刀,问你能否把切开的三个串拼成A. 哈希显然. #include<cstdio> #include<cstring> using namespac ...

  2. bzoj 4506: [Usaco2016 Jan]Fort Moo

    4506: [Usaco2016 Jan]Fort Moo Description Bessie is building a fort with her friend Elsie. Like any ...

  3. IRC程序学习

    %%聊天的中转站,将{chan,MM,Msg}形式的信息转化为 {mm, MM, Msg}形式 -module(mod_chat_controller). -export([start/3]). -i ...

  4. Problem D: 统计元音字母数

    #include<stdio.h> int main() { ]; int n,j,k,a,e,i,o,u; a=e=i=o=u=; gets(c); ;c[k]!='\0';k++) { ...

  5. Codeforces Round #345 (Div. 2) A. Joysticks dp

    A. Joysticks 题目连接: http://www.codeforces.com/contest/651/problem/A Description Friends are going to ...

  6. spring boot 添加自定义属性

    1.添加jar compile('org.springframework.boot:spring-boot-configuration-processor:1.2.0.RELEASE') 2.在app ...

  7. JavaScript中Object.prototype.toString方法的原理

    在JavaScript中,想要判断某个对象值属于哪种内置类型,最靠谱的做法就是通过Object.prototype.toString方法. ? 1 2 var arr = []; console.lo ...

  8. Lua学习之类型与值

    Lua是一种动态语言,在语言中没有类型定义的语法. 在lua中有8中基本的类型: 1.nil(空) 2.boolean 3.number(数字) 4.string(字符串) 5.userdata(自定 ...

  9. 解决kylin报错 ClassCastException org.apache.hadoop.hive.ql.exec.ConditionalTask cannot be cast to org.apache.hadoop.hive.ql.exec.mr.MapRedTask

    方法:去掉参数SET hive.auto.convert.join=true; 从配置文件$KYLIN_HOME/conf/kylin_hive_conf.xml删掉 或 kylin-gui的cube ...

  10. Kubernetes 1.7版本安装

      为什么搞完kubernetes 1.5又要装1.7, :( 是因为微服务架构istio的要求,而且直接用yum安装怎么都是1.5,所以只能通过下载包并且改配置文件的方式了,也好,花两天时间把整个过 ...