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.
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:Perhaps you’d want to store those subviews in an
NSArrayand loop over that array invokingremoveFromSuperviewon each element in that array.