简体   繁体   中英

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.

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]]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM