Does the view view automatically move it to the new supervisor from the old supervisor?

This is not clear, I don’t care if the UIView is automatically freed from its supervisor, if you use addSubview to add it to another view?!? I know that only ONE supervisor can have an idea, but I don’t quite understand how to move the sub-view from one supervisor to another.

save , release (from the old supervisor), addSubview to the new supervisor

or

just addSubview to the new supervisor?

+3
source share
3 answers

, -removeFromSuperView, , UIView -removeFromSuperView. , -addSubView: -removeFromSuperView, -removeFromSuperView UIView.

-removeFromSuperView . , (, , ) . , , , / .

+4

, .

[subview retain]; // because next line will release
[subview removeFromSuperView]; // released by superview
[otherSupView addSubview:subview]; // retained by new superview
[subview release]; // because you retained it in line 1
+2

, , :

[[theView retain] autorelease];
[theView removeFromSuperview];
[newSuperview addSubview:theView];

If the supervisor is the only object that has a save in the preview, then the subview will be released when it is removed from the supervisor. Saving and abstract guarantee that the view will not be released as long as you use it in a function (because it has additional saving on it).

+2
source

Source: https://habr.com/ru/post/1712630/


All Articles