rufus sdkfla
rufus sdkfla

Reputation: 23

iOS: Removing one view from superview causes another to get removed?

This is a very odd problem. I have 5 subviews added to a UIViewController. One of them needs to be removed, but when I do this, one of the remaining 4 subviews is also removed. This necessitates that I re-add it using addSubview. The two views in question are not related in any way.

Is this a known iOS SDK bug? It happens for sure running on the simulator with iOS 6.1.

Thanks.

Upvotes: 2

Views: 1099

Answers (2)

iPatel
iPatel

Reputation: 47059

Here, In Your Question not mention That which Method you use for remove subView so,I give you simple suggestion for remove subView.

Give Tag of Each subView such like,

self.subView1.tag = 1;
self.subView2.tag = 2;
.
.
.
.
self.subViewN.tag = N;

And You can access(Remove) any subView base on its Tag, such like

[[self.view viewWithTag:1] removeFromSuperview];

This tips might helpful for you.

Upvotes: 2

Vinayak Kini
Vinayak Kini

Reputation: 2919

You can remove single subview using the following code.

  [subview_Name removeFromSuperview];

if you want to remove all subviews form the view then use this.

  NSArray *subViewArray = [self.view subviews];
  for (id obj in subViewArray)
  {
   [obj removeFromSuperview];
  }

if you want to remove all subview of particular class then use this.

  NSArray *subViewArray = [self.view subviews];
  for (id obj in subViewArray)
  {
   if([obj isKindOfClass:[classname class]])
      {
          [obj removeFromSuperview];
      }

  }

example : if you want to remove subview of UIImageView class then replace if condition with this.

[obj isKindOfClass:[UIImageView class]]

Upvotes: 2

Related Questions