27

I have added UIButton and UITextView as subviews to my view programmatically.

notesDescriptionView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,460)];
notesDescriptionView.backgroundColor = [UIColor redColor];
[self.view addSubview:notesDescriptionView];

textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,320,420)]; 
[self.view addSubview:textView]; 
printf("\n description  button \n");

button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button
  addTarget:self action:@selector(cancel:)
  forControlEvents:UIControlEventTouchDown];
[button setTitle:@"OK" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 420.0, 160.0, 40.0);
[self.view addSubview:button];

I need to remove all subviews when the button is clicked.

I have tried:

[self.view removeFromSuperView]

but it's not working.

Cœur
  • 37,241
  • 25
  • 195
  • 267
mac
  • 4,760
  • 7
  • 31
  • 33

3 Answers3

59

to remove all the subviews you added to the view

use the following code

for (UIView *view in [self.view subviews]) 
{
    [view removeFromSuperview];
}
yoAlex5
  • 29,217
  • 8
  • 193
  • 205
mac
  • 4,760
  • 7
  • 31
  • 33
23

I assume you're calling [self.view removeFromSuperView] from a method in the same class as the above snippet.

In that case [self.view removeFromSuperView] removes self.view from its own superview, but self is the object from whose view you wish to remove subviews. If you want to remove all the subviews of the object, you need to do this instead:

[notesDescriptionView removeFromSuperview];
[button.view removeFromSuperview];
[textView removeFromSuperview];

Perhaps you'd want to store those subviews in an NSArray and loop over that array invoking removeFromSuperview on each element in that array.

Frank Shearar
  • 17,012
  • 8
  • 67
  • 94
  • what about the button, i have created , by adding as sub view. actually i am trying to show a view with textview,when button pressed , after when i click the another button that is in present view, it has to go back my previous view. – mac Jun 09 '10 at 12:28
  • i have tried [notesDescriptionView removeFromSuperview]; [textView removeFromSuperview]; but its not working. – mac Jun 09 '10 at 12:37
  • What is the right way to call such a method that removes subview from superview from other class? I'm trying delegate method but it doesn't work. – SanitLee Sep 26 '16 at 01:23
8

I've always been surprised that the Objective-C API doesn't have a simple method for removing all sub views from a UIView. (The Flash API does, and you end up needing it quite a bit.)

Anyway, this is the little helper method that I use for that:

- (void)removeAllSubviewsFromUIView:(UIView *)parentView
{
  for (id child in [parentView subviews])
  {
    if ([child isMemberOfClass:[UIView class]])
    {
      [child removeFromSuperview];
    }
  }
}

EDIT: just found a more elegant solution here: What is the best way to remove all subviews from you self.view?

Am using that now as follows:

  // Make sure the background and foreground views are empty:
  [self.backgroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  [self.foregroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

I like that better.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Erik van der Neut
  • 2,245
  • 2
  • 22
  • 21