The problem is in your code, when you call removeFromSuperview , the view will be released by the parent view. No need to call removeFromSuperview , just add it as a subtitle of another view to remove it from the current parent.
Therefore use:
for (UIView *view in self.view.subviews) { if (view.tag != 666) { [self.contentView addSubview:view]; } }
According to the UIView class reference :
addSubview
Adds a view to the end of the list of recipients for the view.
- (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 saves the view and sets its next responder to the receiver, which is its new overview.
Views can only have one view. If the view already has a supervisor and this view is not the recipient, this method deletes the previous one before making the recipient a new supervisor .
source share