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: