How can I create one UIViewController with two views that display one or the other depending on button clicked

You should not have two main views in a single view controller, instead you need to create one view controller per view that you want to show. However you can certainly have multiple subviews in a single view controller, which may be what works for you.

There are a number of approaches to solve this the problem, the correct approach would be to create a container UIViewController, and add as its childs the 2 viewcontrollers you want to show, them simply set the view to the view controller you want to display, but that would probably be overly complicated since you mention you are new to iOS development.

Therefore an easy solution (not sure if you can implement this in storyboard - since I don't like it), would be to have a single view controller, with the tabs, and 2 subviews of the main view, then you can simply switch between views by doing something like this:

[self.view addSubview:view1];

//to switch

[view1 removeFromSuperview];
[self.view addSubView:view2];

alternatively, you do not really need to remove it from superview but just hide it, and then use bringSubViewToFront to show the view that you need.

If you want to use the other approach I would recommend looking for this video the WWDC 2011 video titled "Implementing UIViewController Containment". This other question should be useful to: UISegmented control with 2 views

Hope that helps.


Using storyboard api you can switch between 2 viewControllers

 - (void)viewDidLoad {
    [super viewDidLoad];
    UIViewController *viewController = [self viewControllerForSegmentIndex:self.typeSegmentedControl.selectedSegmentIndex];
    [self addChildViewController:viewController];
    viewController.view.frame = self.contentView.bounds;
    [self.contentView addSubview:viewController.view];
    self.currentViewController = viewController;
}
- (UIViewController *)viewControllerForSegmentIndex:(NSInteger)index {
    UIViewController *viewController;
    switch (index) {
        case 0:
            viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];
            break;
        case 1:
            viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
            break;
    }
    return viewController;
}
- (IBAction)segmentChanged:(UISegmentedControl *)sender {
    UIViewController *viewController = [self viewControllerForSegmentIndex:sender.selectedSegmentIndex];
    [self addChildViewController:viewController];
    [self transitionFromViewController:self.currentViewController toViewController:viewController duration:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
        [self.currentViewController.view removeFromSuperview];
        viewController.view.frame = self.contentView.bounds;
        [self.contentView addSubview:viewController.view];
    } completion:^(BOOL finished) {
        [viewController didMoveToParentViewController:self];
        [self.currentViewController removeFromParentViewController];
        self.currentViewController = viewController ;
    }];
    self.navigationItem.title = viewController.title;
}

This is in reference to iOS tutorial by Raywenderlich. Hope this helps