问题
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