3

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!

2 Answers 2

2

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];
Sign up to request clarification or add additional context in comments.

3 Comments

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.
I would expect so. But if not you could always call them yourself manually
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.
2

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.

6 Comments

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.
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...
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."
If you do that then you'll have strange results. I'd just put a log statement in that method and see if it gets called when just adding & removing from a another view's subviews. If not, you can call it yourself in the method above...
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...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.