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

addSubview:算法

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

译:增长一个视图到接收者的子视图列表中。数据结构

 

- (void)addSubview:(UIView *)viewapp

Parametersthis

viewspa

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

译:view参数表明被增长的view,这个view会被它的接收者retain一次(即引用计数+1)。增长完成以后,这个view将出如今接收者的其余子视图的上面。索引

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

Discussionrem

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的操做会把它先从上一个父视图中移除(包括响应者链),再加到新的父视图上面。


总结: 把两个内存相同的View,addSubview一个View上,最后不是两个View的叠加,而是就是加上了一层!

 

Availability

  • Available in iOS 2.0 and later.

See Also

    • – insertSubview:atIndex:
    • – insertSubview:aboveSubview:
    • – insertSubview:belowSubview:
    • – exchangeSubviewAtIndex:withSubviewAtIndex:
相关文章
相关标签/搜索