This is just to confirm my solution: I have two UIView that I would like to swap between controlled by the UIViewController
In IB I have created 3 UIViews: TopView A_View B_View
Then in ViewDidLoad I add [
[self addSubView] A_View];
Then in some IBAction method I do this
[A_View removeFromSuperview];
[self.view addSubview:B_View];
This is indeed swapping the two views, with TopView just sitting there. But my first thought was just to call [[self view] removeFromSuperView]]; and then add B_view.
This gives me an empty screen?
So is my solution the right way?
Thanks in advance Regards Christian
Looks fine to me. It's a pretty normal swapping style, just remove one and add the other.
You should always watch out with modifying the UIViewController's view itself. Your solution is fine.
That way is fine and perfectly straightforward.
If you're on iOS 4.0 or later, you can also use the transitionFromView:toView:duration:options:completion: method in UIView, which removes and adds for you, plus provides a transition animation.
The reason [[self view] removeFromSuperview] left you with a blank window is because you're removing your controller's base view object (including it's subviews, e.g. Top_View, etc) from it's superview (likely the window object). Your solution works when you remove A_View and add B_View, as both are being added or removed as subviews to your controller's view, i.e. self.view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With