views:

145

answers:

2

Hi

I have added UIButton,UITextView as subview to my view, programatically.

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

here if we click the button , i need to remove the all the sub views,

i am trying

[self.view removeFromSuperView]

but its not working

any help.

A: 

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
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
i have tried [notesDescriptionView removeFromSuperview];[textView removeFromSuperview];but its not working.
mac
+3  A: 

to remove all the subviews you added to the view

use the following code

for (UIView *view in [self.view subviews]) { [view removeFromSuperview]; }

mac