關於addSubView需要注意的幾個點
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: