taoru's memo

Objective-c,cocos2d,など開発についてのメモ(andoroidも少しだけ)

既にsuperviewを持っているviewを他のviewにaddSubviewした場合の振る舞い

あるUIViewがaddSubviewしたときに、すでにsuperviewを持っている(addSubviewされている)viewを貼り付けようとした場合の振る舞いを初めて知った。

UIView Class Referenceによると

UIView Class Reference

Discussion
This method retains view and sets its next responder to the receiver, which is its new superview.

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.

つまり、既にaddされているviewがもう一度他のviewにaddされそうになったとき、
親をすげ替えてくれる。


これを知らなくて、今まではわざわざremoveFromSuperviewしてから張り替えてた…
cocos2dではaddChildを連続で呼び出すとクラッシュするので、UIViewでもそうなんだろうなと勝手に思ってた。


以前もUIViewの振る舞いを知らずに同じような記事書いてる。
InsertSubviewしたときはaddSubviewはいらない - taoru's memo

ClassReference分かりやすいし一度全部読んでみようかな…

QLOOKアクセス解析