Search code examples
iosobjective-cios5

What is the correct way to handle multiple view controllers on the same screen?


I'm having a view controller, that has few controls and images, and is located on top of the screen. I will have a space below it for child view controllers and their views.

I will show one child view controller at a time, what is a good way to do that? Total amount of child view controllers is around 6, they are very different, so reusing some container view controller won't really work.

When pressing some button on these controllers I will move to next one. Should I make some property, let's say contentView that will hold a view of the controller that is currently on screen? How do I handle rotation if I don't use auto layout?

EDIT: This is more a theory question, I know of methods addChildViewController and know the way how I add views and controllers to their parents. I just want to know the good way to do that.


Solution

  • It depends somewhat on how you want to transition between the different child view controllers, but your question already lists a good approach.

    You definitely want a different view controller for each of the children. Add a container view to your top level view. This view is where rotation and auto-resizing are handled. The contents of this view could be the child view controllers views themselves (and you control the transitions using one of the methods like transitionFromView:toView:duration:options:completion:) or the container view could hold a UINavigationController and you just push the child view controllers into it.

    Whatever the container view holds, you need to take care if any of your child view controllers tries to present another view controller as a modal. The presented view controller needs to be presented by the view controller at the top of the hierarchy or the presented view may no interact correctly or truly be presented in front of other screen elements.