简体   繁体   English

如何在一个视图控制器中将xib加载到两个容器视图中

[英]How to load xib into two Container Views within one view controller

I want to create a single person lap timer xib/class and load it twice onto a single View Controller on my Story Board. 我想创建一个单人单圈计时器xib / class,并将其两次加载到我的故事板上的单个View Controller中。 Each of the two instances will be used to time and compare two persons lap times on a single ViewController. 两个实例中的每一个都将用于在单个ViewController上计时和比较两个人的圈速。

I have laid out two Container Views side by side within the Story Board View Controller (LapCounterViewController) 我在故事板视图控制器(LapCounterViewController)中并排布置了两个容器视图

I have also created an xib and class files as a single person lap timer ( LapCounterNibViewContainer) 我还创建了一个xib和类文件作为单人计时器(LapCounterNibViewContainer)

How do I create two instances of LapCounterNibViewContainer and put it inside each of the Container Views 如何创建LapCounterNibViewContainer的两个实例并将其放入每个容器视图中

_vc1 = [[LapCounterNibViewController alloc] initWithNibName:@"LapCounterNibViewController"  bundle:nil];
_vc1.view.frame = self.LapCounterFrame1.frame;
//_vc1.delegate = self;

[_LapCounterFrame1 addChildViewController:_vc1];
[_vc1 didMoveToParentViewController:self];
[self.view addSubview: _vc1.view];

在此处输入图片说明

在此处输入图片说明

In the storyboard, you can add two container views to the same view controller and connect them both with the same child view controller by right-click dragging and choosing embed. 在情节提要中,您可以将两个容器视图添加到同一视图控制器,然后通过右键单击并选择嵌入将它们都与同一子视图控制器连接。 This created the segue like so: 这样创建了segue:

嵌入segue屏幕截图

Click on the segue and give it an identifier. 单击segue并为其指定一个标识符。 Then, add the prepareForSegue method to your parent view controller, and set some properties for the lap timers separately if you want. 然后,将prepareForSegue方法添加到父视图控制器,并根据需要分别设置圈速计时器的一些属性。

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segueName isEqualToString: @"embedSegueToLapTimerOne"]) {
        LapCounterNibViewController * childViewController = (LapCounterNibViewController *) [segue destinationViewController];
        [childViewController setFoo:bar1];
    }
    if ([segueName isEqualToString: @"embedSegueToLapTimerTwo"]) {
        LapCounterNibViewController * childViewController = (LapCounterNibViewController *) [segue destinationViewController];
        [childViewController setFoo:bar2];
    }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM