Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove current view from superview

Tags:

ios

iphone

Let's say I have a viewcontroller "ViewBViewController". In that viewcontroller I create an instance of the "ViewAViewController" and use the addSubView: method to display this ViewAViewController. It then processes a bunch of information, and is now done. I want it to automatically get removed as a subview when it's done.

I was looking at the removeFromSuperview method, but can't seem to call that from within the viewcontroller whose view I'm trying to remove (my first instinct was [self.view removeFromSuperview], but that gets rid of the entire view, not just the subview I'm after).

The only way I can think of is setting up a delegate protocol, and have View B take care of the unloading of View A on behalf of View A as its delegate. However this approach seems a bit overkill. Am I missing an easier solution?

Thanks in advance!


2 Answers

UIViewController does not respond to removeFromSuperview, because a UIViewController is not a UIView but a UIViewController. No surprises there. You can call removeSuperview on any view, such as the view associated to a view controller (here self):

[self.view removeFromSuperview];

or if you just want to remove one subview:

[mySubview removeFromSuperview];

or if your subview is a member of self (i.e. declared in the interface say):

[self.mySubview removeFromSuperview];
like image 173
SK9 Avatar answered Aug 19 '26 01:08

SK9


Have you tried: setHidden: YES ?

like image 32
WrightsCS Avatar answered Aug 19 '26 01:08

WrightsCS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!