The issue in your code is, when you call removeFromSuperview
the view will be released by the parent view. No need of calling removeFromSuperview
, just add it as subview of another view will remove it from it's current parentView.
So use:
for (UIView *view in self.view.subviews)
{
if (view.tag != 666)
{
[self.contentView addSubview:view];
}
}
According to UIView Class Reference:
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.
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.
self.contentView
is linked to nib and is notnil
? – Cladoceran