關於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:
相關文章
相關標籤/搜索