spinlock
spinlock

Reputation: 231

Multiple views and controllers in UITabBarController

I'm trying to add multiple views inside a UITabBarController. Currently my object hierarchy looks like this: UITabBarController -> UIViewController* -> UIView*. As a more concrete example, the first view controller for my UITabBarController is a UIViewController, and that has three subviews, which are controlled by a UISegmentedControl. Depending on what segment is selected, I push the corresponding view to the front.

I understand that I can use a UINavigationController to manage my three views; however, the data I wish to present is not really hierarchical.

Are there examples of container controllers other than UITabBarController or UINavigationController that I can use for this case? Or is there another approach I should use (I'm currently managing views manually).

Thanks!

Upvotes: 0

Views: 809

Answers (2)

There's not really any supporting framework for this - usually you have to manage switching out views in a switched container view yourself.

One approach I have taken in the past is to maintain an array of ViewControllers for each switching view, and take the viewController.view to add as a subview of your switched container view. Then I write code around the switching of the view controllers to call viewWillAppear and viewWillDisappear on the contained view controllers as they are swapped in and out, that makes things much simpler since you can treat them totally separately.

You can write that class kind of generically and then re-use it.

Upvotes: 0

Ben S
Ben S

Reputation: 69412

Custom view controllers are covered in the View Controller Programming Guide.

If you wanted to change your layout to use the UINavigationController you could remove the segmented control view and have the first view be a table view inside a nav controller. The table would have the three options the segmented control had and tapping on them would push the view associated with that option. This way you've created a hierarchical view layout rather than using the segmented control, which is typically used to toggle functionality rather than control views.

If you choose to do this, these two guide sections would be a good place to start.

Upvotes: 1

Related Questions