关于数组中加入相同的view的试验
随便新建一个工程,然后在控制器中粘贴如下代码
- (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的试验的更多相关文章
- Vue 改变数组中对象的属性不重新渲染View的解决方案
Vue 改变数组中对象的属性不重新渲染View的解决方案 在解决问题之前,我们先来了解下 vue响应性原理: Vue最显著的一个功能是响应系统-- 模型只是一个普通对象,修改对象则会更新视图.受到ja ...
- Android中自定义样式与View的构造函数中的第三个参数defStyle的意义
零.序 一.自定义Style 二.在XML中为属性声明属性值 1. 在layout中定义属性 2. 设置Style 3. 通过Theme指定 三.在运行时获取属性值 1. View的第三个构造函数的第 ...
- 解析plist文件(字典里包着数组,数组中又包含字典)
#import "RootTableViewController.h" #import "City.h" @interface RootTableViewCon ...
- php从数组中取出一段 之 array_slice
array array_slice ( array $array , int $offset [, int $length [, bool $preserve_keys ]] ) array_slic ...
- js从数组中随机取出不同的元素
前言 上午处理个需求需要从一个总数组中随机取出不同的元素.共使用两个方法.第一种方法较常规,经测试有bug,数据量大以后随机几次返回的对象直接是function而不是object. 当然简单数据类型应 ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- 【POJ】1089Intervals
Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8276 Accepted: 3270 Descrip ...
- BZOJ 2818 Gcd(莫比乌斯反演)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2818 [题目大意] 给定整数N,求1<=x,y<=N且Gcd(x,y)为素 ...
- [NOI2017]整数
[NOI2017]整数 题目大意: \(n(n\le10^6)\)次操作维护一个长度为\(30n\)的二进制整数\(x\),支持以下两种操作: 将这个整数加上\(a\cdot2^b(|a|\le10^ ...
- 找出最小元素 Exercise07_09
import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:找出最小元素 * */ public class Exercise07_0 ...
- 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A. Orchestra 水题
A. Orchestra 题目连接: http://www.codeforces.com/contest/635/problem/A Description Paul is at the orches ...
- Educational Codeforces Round 8 A. Tennis Tournament 暴力
A. Tennis Tournament 题目连接: http://www.codeforces.com/contest/628/problem/A Description A tennis tour ...
- php根据汉字获取拼音(php基于拼音搜索实现原理)
php根据汉字获取拼音(php基于拼音搜索实现原理) 代码一:获取字符串汉字首字母,兼容GBK和UTF-8 <?php function getfirstchar($s0){ //获取单个汉 ...
- 通过现有数据导出新表SQL
Date: 20140217 Auth: JIN 需求: 导出一个表的两个列的表的SQL语句(包含数据) 方法:创立一个临时表 mysql> desc kw_keywords;+-------- ...
- jquery-qrcode 生成和读取二维码
首先要导入jar包(生成二维码的jar和读取二维码的jar) 生成二维码: package com.imooc.qrcode; import java.awt.Color; import java.a ...
- [转]SSIS - Connect to Oracle on a 64-bit machine (Updated for SSIS 2008 R2)
本文转自:http://sqlblog.com/blogs/jorg_klein/archive/2011/06/09/ssis-connect-to-oracle-on-a-64-bit-machi ...