3

I'm trying to follow the View Controller Programming Guide for iOS to implement a container view in my application. At the moment I'm just trying to get an initial first view to load but the label included in the first controller is not being shown. In the end, I hope to be able to control which view is shown in the container by using a segmented control.

Any help would be greatly appreciated.

My Storyboard

Storyboard Screenshot

ViewController.h

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIView *contentController;

- (IBAction)SegmentedControlValueChange:(UISegmentedControl *)sender;

@end

ViewController.m

#import "ViewController.h"

#import "FirstController.h"
#import "SecondController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    FirstController *firstController = [[FirstController alloc] init];
    [self displayContentController:firstController];
}

- (IBAction)SegmentedControlValueChange:(UISegmentedControl *)sender
{
}

- (void)displayContentController: (UIViewController *)content
{
    [self addChildViewController:content];

    content.view.frame = [self frameForContentController];

    [self.view addSubview:content.view];

    [content didMoveToParentViewController:self];
}

- (CGRect)frameForContentController
{
    return self.contentController.bounds;
}

@end
ritch
  • 1,722
  • 12
  • 37
  • 64
  • I have up voted your question because your code snippet has been very great help to me. So many thanks for including your code in the question. But I can't tell which one of the scenes with subtitle "View Controller" that the code is referring to. At first I image the code belonged to the second "View Controller" but then I see `(UISegmentedControl *)sender` in the `.h` file. I am referring to the sub labels: "View Controller" -> "View Controller" ->["First Controller", "Second Controller"]. – Katedral Pillon Jul 23 '14 at 15:23
  • I am trying to implement the UISegmentedControl portion. – Katedral Pillon Jul 23 '14 at 15:25
  • The ViewController refers to the first one shown in the storyboard. You don't need the other one, you can delete it. – ritch Jul 24 '14 at 08:37
  • @KatedralPillon I have provided an answer on your question - http://stackoverflow.com/a/24929501/273173 – ritch Jul 24 '14 at 09:12

1 Answers1

3

If FirstController is part of the storyboard, then you'll have to load it from the storyboard.

Try doing

FirstController *firstController = [self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"];

John Grant
  • 1,629
  • 10
  • 8