Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewController multiple UIView

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

like image 420
Chris G. Avatar asked May 19 '26 11:05

Chris G.


2 Answers

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.

like image 105
Tom van der Woerdt Avatar answered May 21 '26 23:05

Tom van der Woerdt


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.

like image 28
mvl Avatar answered May 22 '26 01:05

mvl



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!