addSubview: Adds a view to the end of the receiver’s list of subviews. 譯:增加一個視圖到接收者的子視圖列表中。 - (void)addSubview:(UIView *)view Parameters view The view ...
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的操作會把它先從上一個父視圖中移除(包括響應者鏈),再加到新的父視圖上面。
總結: 把兩個記憶體相同的View,addSubview一個View上,最後不是兩個View的疊加,而是就是加上了一層!
Availability
- Available in iOS 2.0 and later.
See Also
- – insertSubview:atIndex:
- – insertSubview:aboveSubview:
- – insertSubview:belowSubview:
- – exchangeSubviewAtIndex:withSubviewAtIndex: