How to implement SegmentedControlValueChange to control Container View

末鹿安然 提交于 2019-12-13 05:47:59

问题


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"?

回答1:


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.



来源:https://stackoverflow.com/questions/24914901/how-to-implement-segmentedcontrolvaluechange-to-control-container-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!