如何实现 SegmentedControlValueChange 来控制容器视图 [英] How to implement SegmentedControlValueChange to control Container View
问题描述
我的问题是对其他人的问题的跟进.在@ritch 提供的问题图片中,他有以下视图控制器
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
{
}
逻辑上我认为这个方法应该在父控制器"中,而作为参考,在子控制器"中我应该有 displayContentController
和
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"];
有人请帮我澄清一下:在 SegmentedControlValueChange 和 instantiateViewControllerWithIdentifier 之间:
Will someone please clarify for me: between SegmentedControlValueChange and instantiateViewControllerWithIdentifier:
- 父控制器"的 h 和 m 文件中包含哪些代码?
- 父控制器"的 h 和 m 文件中包含哪些代码?
推荐答案
最后我对我在问题中陈述的内容采取了不同的方法,因为我觉得使用故事板标识符有点丑陋"和不正确,因为视图没有转到他们那里去.
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.
这就是我所做的:
我首先为要在容器视图中显示的视图控制器创建带有 XIB 文件的类.(例如 FirstController、SecondController 等.)
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..)
然后我把它放在我的 ViewController 的 ViewDidLoad 方法中(父视图控制器 - 带有分段控件的那个)
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]];
}
希望这会有所帮助.
这篇关于如何实现 SegmentedControlValueChange 来控制容器视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!