addSubview:

Adds a view to the end of the receiver’s list of subviews.

译:增加一个视图到接收者的子视图列表中。

- (void)addSubview:(UIView *)view

Parameters

view

The view to be added. This view is retained by the receiver. After being added, this view appears on top of any other subviews.

译:view参数代表被增加的view,这个view会被它的接收者retain一次(即引用计数+1)。增加完成之后,这个view将出现在接收者的其他子视图的上面。

ps:关于子视图的出现的层次的问题,可以从这些子视图被保存的数据结构来探寻答案
,每个视图都有个数组的属性,subviews,这个就是保存的它的子视图的引用。而这个数组的顺序就是代表了各个子视图被加入时的顺序。index=0
的就是最先被加入进去的,以此类推。所以,索引值越高的视图越不容易被覆盖。

Discussion

This method retains view and sets its next responder to the receiver, which is its new superview.

译:这个方法会retain一次view,并且设置它的下一个响应者是receiver,即它的新的父视图。

ps:在removeFromSuperview里已经说过,其实视图直接的操作往往牵涉到两个方面的操作,一个是视图的数据结构,一个是响应者链。当然,addsubview也不例外。

Views can have only one superview. If view already has a
superview and that view is not the receiver, this method removes the
previous superview before making the receiver its new superview.

译:每一个视图只能有唯一的一个父视图。如果当前操作视图已经有另外的一个父视图,则addsubview的操作会把它先从上一个父视图中移除(包括响应者链),再加到新的父视图上面。

Availability

  • Available in iOS 2.0 and later.

See Also

    • – insertSubview:atIndex:
    • – insertSubview:aboveSubview:
    • – insertSubview:belowSubview:
    • – exchangeSubviewAtIndex:withSubviewAtIndex:

addSubview和insertSubview的区别

addSubview 是将view加到所有层的最顶层

相当于将insertSubview的atIndex参数设置成view.subviews count

[view addSubview:oneview] == [view insertSubview:oneview atIndex:view.subviews count]

addSubview是加到最后
insertSubview是加到指定的位置

 

为什么要在addsubview:一个view对象后,release它?

先看代码:

1
2
3
IMGView *imgView = [[IMGView alloc] initWithFrame:CGRectMake(10, 0, 300, 300)];
[self.view addSubview:imgView];
[imgView release];

为什么imgView要release呢?可能很多人跟我一样,之前不是很清楚。 我们逐行分析一下

第一行,alloc一次,imgView对象retainCount为1,

二行,addSubview一次,此方法会把你传过去的对象retain一次,那么此时它的retainCount为2。self.view变为它的第二
个待有者。参考:The receiver retains view. If you use removeFromSuperview to
remove view from the view hierarchy, view is released.
第三行,调用release方法,此处释放对imgView的所有权,retainCount减1。

到语言句尾imgView的所有者只剩下self.view,并且它的retainCount仅为1。内存管理第一铁则,谁retain(alloc,copy)谁release(autorelease)。上述的做法也是为了符合这一准则。

addSubView需要注意的几个点的更多相关文章

  1. 关于addSubView需要注意的事项 -今天吃了一个大亏

    addSubview: Adds a view to the end of the receiver’s list of subviews. 译:增加一个视图到接收者的子视图列表中. - (void) ...

  2. addsubview跟insertsubview的区别

    子视图是以栈的方式存放的. 每次addsubview时都是在最后面添加. 每次在addsubview前和addsubview后可以看看[self.view.subViews count]: 你看看你的 ...

  3. pushViewController addSubview presentModalViewController视图切换

    1.pushViewController和popViewController来进行视图切换,首先要确保根视图是NavigationController,不然是不可以用的, pushViewContro ...

  4. addChildViewController 与 addSubview

    在viewcontrollerA中, 如果想把controllerB.view添加进来, 可以用 addSubview, 但如果controllerB中有个事件, 使用到 self.navigatio ...

  5. iOS: 神奇的addSubView

    看着addSubView, 本以为是添加多个对象, 但通过测试代码, 发现同一个对象在addSubView中只会添加一次. 想想, 视图对象是通过引用得到的. 在视图的子视图集中, 只保存一个相应的对 ...

  6. 在[self addsubView:xxx]中,self.name 和 _name的区别

    在[self addsubView:xxx]中,self.name 和 _name的区别self.name 会调用重写的getter方法,而_name添加的只是_name 这个成员变量

  7. 【编程技巧】addSubview和insertSubview的区别

    addSubview 是将view加到所有层的最顶层 相当于将insertSubview的atIndex参数设置成view.subviews count 即 [view addSubview:onev ...

  8. 如何处理UIVIew addsubview 不显示subview

    老代码: addsubview不显示uilabel -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSIn ...

  9. [self.view addSubview:vc2.view]程序崩溃的解决办法

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIButt ...

随机推荐

  1. gprof使用介绍

    gprof 1.1      简介 gprof实际上只是一个用于读取profile结果文件的工具.gprof采用混合方法来收集程序的统计信息,他使用检测方法,在编译过程中在函数入口处插入计数器用于收集 ...

  2. mysql学习之-show table status(获取表的信息)参数说明

    --获取表的信息mysql> show table status like 'columns_priv'\G;*************************** 1. row ******* ...

  3. MySQL 5.5: InnoDB Change Buffering

    To speed up bulk loading of data, InnoDB implements an insert buffer, a special index in the InnoDB ...

  4. Python 列表如何获得一个指定元素所在的下标

    在使用Python3.4读取txt数据到列表,由于编码问题,读取到的数据通常会出现'\ufeffX'(x为你想要的数据).这时候如果需要把列表中的数据由字符串转换到数值型的数据的进行数据分析的话就会出 ...

  5. 安装SQL Server驱动到Maven仓库[转]

    from:http://raysen.blog.51cto.com/324335/1159232 Maven does not directly support some libraries, lik ...

  6. 【转】JS跨域(ajax跨域、iframe跨域)解决方法及原理详解(jsonp)

    这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...

  7. EditText的圆角与边框

    先看一下效果图: 先创建一个xml文件edittext_shape.xml: <?xml version="1.0" encoding="utf-8"?& ...

  8. Subversion与TortoiseSVN的安装

    首先介绍一下Subversion与TortoiseSVN两者之间的关系: Subversion是一种集中分享信息的系统,它的核心是版本库,储存所有的数据.版本库按照文件树形式储存数据-包括文件和目录. ...

  9. session.flush加锁测试.

    测试结论 1 session.flush (用于提交SQL执行计划. hibernate会给数据库加锁, 执行效果等同于select for update的锁级别.如果是oracle 默认为lock ...

  10. LintCode "Previous Permutation"

    A reverse version of the Dictionary algorithm :) If you AC-ed "Next Permutation II", copy ...