开发者

Howto switch between UIViewControllers without using a tab bar?

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开发者_Go百科 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!


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];


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜