简体   繁体   中英

Switching between UIViewControllers with UISegmentedControl

Right I have looked at a few SO questions on the subject and I am finding it difficult to come up with the correct solution here.

Requirements

I have a UITabBar based application. One of the tabs has a UINavigation controller with UISegmentedControl at the top allowing the user to switch between three different views. Each view will have a UITableView which will allow the user to navigate to another view. These views should be pushed onto to the navigation controller.

Problem

Now all the SO questions and Answers on the subject show how to switch between views. However I need the view I switch to, to allow pushing of another view onto the navigation stack. I am not sure this is even possible. I thought about UIViewController containment - however that would show a view being pushed onto the stack in a smaller window that the screen's bounds. Not what I am looking for.

Any ideas how I can solve this with storyboards and UIViewControllers?

UPDATE

Here is what I am trying to do: In the screenshot the container area is where I need to load other view controllers into. The UISegment control cannot go into the navigation bar as that space is used for something else. So that's why I think UIViewController containment might be better here?

在此输入图像描述

So even though this isn't using separate TableViewControllers , you can use different custom UIViews that are hidden by default and become visible when you select it's corresponding button. This will unfortunately make it so you have all three view's logic in the same VC.

To get around this, you can try setting up some delegates and mimicking the TableViewController logic separation by sending out the didSelectTableAtIndexPath , UIGesture touches, etc into classes outside the ViewController to help keep your code cleaner.

User UITabBarController and hide the tab bar.

- (void)viewDidLoad
{
     self.tabBar.hidden = YES;
}

Binding the segment control with method valueChanged

- (void)valueChanged:(UISegmentedControl *)seg
{
    if ([seg.selectedSegmentIndex == 0]) {
        self.selectedIndex = 0;
    } else if ([seg.selectedSegmentIndex == 1] {
        self.selectedIndex = 1;
    }
}

I achieve this by this way, I hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM