I have a UIButton and I want to add it to multiple UIViews. Later on – I want to remove it from one of the superviews. I am trying it the following way but when I pass the removeFromSuperview message to the button, it gets removed from both the views:
[viewOverlay addSubview:myButton];
[viewButtons addSubview:myButton];
Afterwards I want it to be removed from the viewOverlay only.
[myButton removeFromSuperview];
and this causes the button removed from both the views. Any idea how can I achieve this???
Obaid
It might look that way, but
myButtonwas removed fromviewOverlaywhen you added it toviewButtons. From the documentation for-addSubview::The only correct solution here is to create two buttons and add one to each of
viewOverlayandviewButtons. The two buttons can look the same, have the same target, and perform the same action, so they’ll look like the “same” button from the user’s perspective. But a view can only have one superview at a time. I mean that literally: every view has asuperviewpointer that points to the view that contains it, and that point can obviously only point to one object at a time.