I have added following piece of code in a view.
- (IBAction)accept_clicked:(id)sender {
[self.view removeFromSuperview];
self.view = nil;
}
Once accept is clicked, I have removed the own view like this. It worked fine, anyhow is it fine to do like this or It should be removed from another view(parent)?
Don’t do this (with
self.view) – it doesn’t looks good and you might face difficult to find problems.self.viewis the main view associated with anUIViewController. So this view to be visible on the screen, you must have shown it somehow: either pushing it to aUINavigationControlleror presenting it modally with-presentViewController:animated:completion:(IOS5+) or- presentModalViewController:animated:. Showing a view by instantiating a view controller and adding its view to the current view controller’s view is not a good practice also:In your particular case I suppose you are showing some terms and conditions (or something similar) and have an accept and deny button, your best solution would be to present your view controller from somewhere, implement a delegate method, so the presenting view controller can have the result and then in your
-accept_clicked:method to use either[self dismissModalViewControllerAnimated:YES]or[self dismissViewControllerAnimated:completion:](IOS5+),