0

My question is a follow up to someone else's question. In the image that @ritch provides with his question, he has the following view controllers

"View Controller" -> (Container View)"View Controller" ->["First Controller", "Second Controller"]

For my question, I will rewrite them as

"Parent Controller" -> (Container View)"Child Controller" ->["First Controller", "Second Controller"]

So I am trying to implement the method

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

Logically I thought this method should be in "Parent Controller" while, for reference, in "Child Controller" I should have displayContentController and

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

Will someone please clarify for me: between SegmentedControlValueChange and instantiateViewControllerWithIdentifier:

  • What code goes in the h and m files of "Parent Controller"?
  • What code goes in the h and m files of "Parent Controller"?
Community
  • 1
  • 1
Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199

1 Answers1

0

In the end I took a different approach to what I stated in my question as I felt using storyboard identifiers was bit 'ugly' and incorrect as the views had no segues going to them.

So here is what I did:

I started off by creating classes with XIB files for the view controllers that were going to be shown in the container view. (e.g. FirstController, SecondController etc..)

I then put this in the ViewDidLoad method of my ViewController (the parent view controller - the one with the segmented control)

- (void)viewDidLoad
{
    [super viewDidLoad];

    // First Controller
    self.firstViewController = [[FirstViewController alloc] init];

    // Second Controller
    self.secondViewController = [[SecondViewController alloc] init];

    // Add the controllers to an Array
    self.controllers = @[self.firstViewController, self.secondViewController];

    // Set the container to show the first view controller on load
    [self displayContentController:[self.controllers firstObject]];
}

I then set up three methods to handle the displaying and hiding of the views for the container view

- (void)displayContentController:(UIViewController *)content
{
    [self addChildViewController:content];
    content.view.frame = [self frameForContentController];
    [self.view addSubview:content.view];
    [content didMoveToParentViewController:self];

    // Set current controller
    self.currentController = content;
}

- (void)hideContentController: (UIViewController*)content
{
    [content willMoveToParentViewController:nil];
    [content.view removeFromSuperview];
    [content removeFromParentViewController];
}

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

Then finally, I handled the event when a different segmented control value is selected.

- (IBAction)segmentedControlValueChanged:(UISegmentedControl *)sender
{
    // Hide current view controller
    [self hideContentController:self.currentController];
    // Show new selected view controller
    [self displayContentController:[self.controllers objectAtIndex:sender.selectedSegmentIndex]];
}

Hope this helps.

ritch
  • 1,760
  • 14
  • 37
  • 65
  • So instead of `“Parent Controller” -> (Container View) “Child Controller” -> [“First Controller”, “Second Controller”]` Are you saying that the new program has `“Parent Controller” -> [“First Controller”, “Second Controller”]`? If yes, then how do you know where the First or Second should appear on the screen? Or is that not a problem because your buttons are in the Navigation Bar, which is not what I have. My navigation bar is not holding the my buttons, the Parent View Controller has them in its body. – Katedral Pillon Jul 25 '14 at 20:43
  • @KatedralPillon It's like '“Parent Controller” -> (Container View) -> [“First Controller”, “Second Controller”]'. If you have the segmented controller in the body, you can just adjust the size of the container view so it's not taking up the full view. – ritch Jul 27 '14 at 08:41