views:

889

answers:

2

Hi all,

I have a UINavigationController. In the 2nd level of my hierarchy, I want to show a view controller with a toolbar in which I inserted a segmented control. Through it the user can select between two "views" of the same page that we can call A and B (like in the Calendar application).

When the user press the A segment, the A view must be shown. When the user press the B segment, the B view must be shown.

A and B are complex views, so I prefer to manage them in two separate view controllers called AViewController and BViewController.

Initially I had thought to insert AViewController and BViewController in a UITabBarViewController, but in the pushViewController:animated: official documentation I read that the pushed view controller "cannot be an instance of tab bar controller."

Do you know how can I switch between AViewController and BViewController without using the UITabBarViewController?

Thank you very much!

+1  A: 

You probably want to looking at a UISegmentView which will give you a few buttons that you can use to alter view contents.

Another option would be using the Info button with a nice flip transition between the views.

A 3rd option would be to have a toolbar button bring your your second view as a modal view, and on that screen have a close button that dismisses itself.

A technical answer

Create a container view controller that holds the UISegment view and 2 view controller instance variables, AViewController and BViewController. Also in your main view controller, have a container view that sets the appropriate frame for the child views. Instantiate both of them in viewDidLoad but only show the one that is currently selected...

-(void)showAppropriateView {

     if([segment selectedIndex] == A_VIEW_SEGMENT) {
         [self.containerView addSubView:aViewController.view];
         [bViewController.view removeFromSuperView];
     } else {
         [self.containerView addSubView:bViewController.view];
         [aViewController.view removeFromSuperView];
     }
}

Call this method when the UISegmentView changes.

Ben Scheirman
Thank you for your answer. Probably I have not explained well my question. I have a clear idea from the graphic point of view, but my problem is technical. The problem is that I have 2 view controllers, AViewController and BViewController, and I want to switch between them when the user tap the corresponding segment. Like in the Calendar application, where the use can tap "list", "day" and "month". Without using a tab bar controller and without using modal view controller. Thanks again.
Larry
I updated it with some more details.
Ben Scheirman
Thanks Ben! Really helpful. Do you know if doing so methods view{Will, Did}Appear: of AViewController and BViewController are called? This is essential for me. I need something that does everything automatically, just like the tab bar controller...
Larry
reading the documentation it would seem so: "viewWillAppear: is called in response to a view being added either directly or indirectly to a window. You add views to a window directly by using the addSubview: method to add them to the window’s view hierarchy."
Larry
Ben Scheirman
Unluckly you are right: I have verified in the practice that methods view{Will, Did}Appear: are not automatically called. Why? Reading the Apple's documentation I realized the opposite...
Larry
Answer: because I call the addSubview: method on the *view*, not on the *window*. When a view controller's view is added to a window, its view{Will, Did}{Appear, Disappear}: methods are called... Really interesting!
Larry
+1  A: 

I would add both of your views as subviews of a view (call it rootView) and use bringSubviewToFront to display them:

// display view A
[rootView bringSubviewToFront:AViewController.view];

// display view B
[rootView bringSubviewToFront:BViewController.view];
pheelicks
Thanks. But doing so, viewWillAppear:, viewDidAppear:, viewWillDisappear: and viewDidDisappear: methods of AViewController and BViewController are called? If yes, this is what I need. Thank you.
Larry
I would expect so. But if not you could always call them yourself manually
pheelicks
I do not want to call them manually. I need something that does everything automatically, just like the tab bar controller... Anyway, now I try. Thank you.
Larry