Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7194297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:20:34+00:00 2026-05-28T20:20:34+00:00

I am trying something very simple but somehow I can’t get it to work.

  • 0

I am trying something very simple but somehow I can’t get it to work. All I try to do is switching between 2 View Controllers using an UISegmentedControl as you can see it for example in the App Store application in the Highlights tab.

I am using iOS5 and Storyboards.

Here’s my Storyboad line up:

enter image description here

So I have a root View Controller and two UITableViews – This 2 TableViews I want to switch.

Here’s how the implementation file looks like

#import "SegmentedLocationViewController.h"
#import "PastEventsLocationViewController.h"
#import "FutureEventsLocationViewController.h"

@interface SegmentedLocationViewController()
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;
@property (strong, nonatomic) NSArray *viewControllers;
@end


@implementation SegmentedLocationViewController

@synthesize segmentedControl = _segmentedControl;
@synthesize viewControllers = _viewControllers;

- (IBAction)indexDidChangeForSegmentedControl:(UISegmentedControl*)segmentedControl
{
    NSLog(@"index: %d", segmentedControl.selectedSegmentIndex);
}

- (void)setupViewControllers
{
    PastEventsLocationViewController *pastEventsLocationViewController = [[PastEventsLocationViewController alloc] initWithStyle:UITableViewStylePlain];
    FutureEventsLocationViewController *futureEventsLocationViewController = [[FutureEventsLocationViewController alloc] initWithStyle:UITableViewStylePlain];

    self.viewControllers = [NSArray arrayWithObjects:pastEventsLocationViewController, futureEventsLocationViewController, nil];
}

- (void)setupUI
{
    [self.segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupViewControllers];
    [self setupUI];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

@end

I can trigger the switch event and can log the currently selected index. But I don’t have any idea where to go from here.

Maybe someone can turn my attention towards a certain direction…?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:20:35+00:00Added an answer on May 28, 2026 at 8:20 pm

    This code works pretty well for your purpose, I use it for one of my new apps.
    It uses the new UIViewController containment APIs that allow UIViewControllers inside your own UIViewControllers without the hassles of manually forwarding stuff like viewDidAppear:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // add viewController so you can switch them later. 
        UIViewController *vc = [self viewControllerForSegmentIndex:self.typeSegmentedControl.selectedSegmentIndex];
        [self addChildViewController:vc];
        vc.view.frame = self.contentView.bounds;
        [self.contentView addSubview:vc.view];
        self.currentViewController = vc;
    }
    - (IBAction)segmentChanged:(UISegmentedControl *)sender {
        UIViewController *vc = [self viewControllerForSegmentIndex:sender.selectedSegmentIndex];
        [self addChildViewController:vc];
        [self transitionFromViewController:self.currentViewController toViewController:vc duration:0.5 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
            [self.currentViewController.view removeFromSuperview];
            vc.view.frame = self.contentView.bounds;
            [self.contentView addSubview:vc.view];
        } completion:^(BOOL finished) {
            [vc didMoveToParentViewController:self];
            [self.currentViewController removeFromParentViewController];
            self.currentViewController = vc;
        }];
        self.navigationItem.title = vc.title;
    }
    
    - (UIViewController *)viewControllerForSegmentIndex:(NSInteger)index {
        UIViewController *vc;
        switch (index) {
            case 0:
                vc = [self.storyboard instantiateViewControllerWithIdentifier:@"FooViewController"];
                break;
            case 1:
                vc = [self.storyboard instantiateViewControllerWithIdentifier:@"BarViewController"];
                break;
        }
        return vc;
    }
    

    I got this stuff from chapter 22 of Ray Wenderlichs book iOS5 by tutorial.
    Unfortunately I don’t have a public link to a tutorial. But there is a WWDC 2011 video titled “Implementing UIViewController Containment”

    EDIT

    self.typeSegmentedControl is outlet for your UISegmentedControl

    self.contentView is outlet for your container view

    self.currentViewController is just a property that we’re using to store our currently used UIViewController

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying something very simple, but for some reason it does not work.
I'm trying to do something very simple, yet I can't seem to find the
I am trying to accomplish something very simple, and I'm hoping someone can point
I'm trying to get something very subtle to work, it looks pretty awful right
I am trying to do something very simple: create a new branch. But I
I'm trying to do something very simple in Jquery which i just cant get
I'm trying to make something very simple.. but I do wrong, and I don't
I'm trying to do something very simple, get the top result URL of a
I am really struggling trying to get something very simple achieved. Essentially, I have
I'm trying to do something that seems like it should be very simple, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.