如何将数据从ViewController传递到Container视图?

时间:2021-11-14 07:11:50

I have a storyboard set up in XCode and have a MainViewController. In the MainViewController I have added a ContainerView which naturally creates a Segue with another VIewController.

我在XCode中设置了一个故事板,并有一个MainViewController。在MainViewController中,我添加了一个ContainerView,它自然地与另一个VIewController创建了一个Segue。

In my MainViewController.m file I have set up data and want to link this data to a label in the ContainerView however I thought I could click on the File's Owner and do this but of course I can't because they are 2 different viewcontrollers now.

在我的MainViewController.m文件中,我已经设置了数据,并希望将这些数据链接到ContainerView中的标签,但我想我可以单击文件所有者并执行此操作但当然我不能,因为它们现在是2个不同的viewcontrollers 。

Can someone please help me because I'm struggling with this. There must be an easy way but I can't crack it!

有人可以帮助我,因为我正在努力解决这个问题。必须有一个简单的方法,但我不能破解它!

Thank you

谢谢

4 个解决方案

#1


26  

You can use prepareForSegue just like any other two controllers -- that method will be called after the two controllers are instantiated, but before either viewDidLoad runs. The other way to do this is to use the parent controller's childViewControllers property (the embedded controller is a child). So, the child will be self.childViewControllers[0].

您可以像任何其他两个控制器一样使用prepareForSegue - 在实例化两个控制器之后但在viewDidLoad运行之前调用该方法。另一种方法是使用父控制器的childViewControllers属性(嵌入式控制器是子控件)。所以,孩子将是self.childViewControllers [0]。

After Edit:

编辑后:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"EmbedSegue"]) {
        MyEmbeddedController *embed = segue.destinationViewController;
        embed.labelString = self.stringToPass;
    }
}

Of course, you have to change the names to what you have. Make sure the name you give to the segue in IB matches the one you check for in the if statement. In this example labelString is a string property you set up in your embedded controller. Then in that controller's viewDidLoad method, you can set the value of the label with that string.

当然,您必须将名称更改为您拥有的名称。确保您在IB中为segue指定的名称与您在if语句中检查的名称相匹配。在此示例中,labelString是您在嵌入式控制器中设置的字符串属性。然后在该控制器的viewDidLoad方法中,您可以使用该字符串设置标签的值。

#2


5  

This is pretty much the same answer as the one by rdelmar only in Swift.

这与rdelmar仅在Swift中的答案几乎相同。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let embeddedVC = segue.destinationViewController as? MyEmbeddedController where segue.identifier == "EmbedSegue" {
        embeddedVC.labelString = self.stringToPass
    }
}

"EmbedSegue" has to the segue identifier you set in Interface Builder.

“EmbedSegue”必须具有您在Interface Builder中设置的segue标识符。

#3


0  

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];
    }
}

I should also mention that because you are using a Container view, prepareForSegue will be triggered when you'll present the ViewController that holds the Container.

我还要提一下,因为您正在使用Container视图,所以当您呈现包含Container的ViewController时,将触发prepareForSegue。

#4


0  

Answer for Swift 4:

Swift 4的答案:

if let controller = segue.destinationController as? MyEmbeddedController, segue.identifier!.rawValue == "EmbedSegue" {
    controller.labelString = self.stringToPass
}

#1


26  

You can use prepareForSegue just like any other two controllers -- that method will be called after the two controllers are instantiated, but before either viewDidLoad runs. The other way to do this is to use the parent controller's childViewControllers property (the embedded controller is a child). So, the child will be self.childViewControllers[0].

您可以像任何其他两个控制器一样使用prepareForSegue - 在实例化两个控制器之后但在viewDidLoad运行之前调用该方法。另一种方法是使用父控制器的childViewControllers属性(嵌入式控制器是子控件)。所以,孩子将是self.childViewControllers [0]。

After Edit:

编辑后:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"EmbedSegue"]) {
        MyEmbeddedController *embed = segue.destinationViewController;
        embed.labelString = self.stringToPass;
    }
}

Of course, you have to change the names to what you have. Make sure the name you give to the segue in IB matches the one you check for in the if statement. In this example labelString is a string property you set up in your embedded controller. Then in that controller's viewDidLoad method, you can set the value of the label with that string.

当然,您必须将名称更改为您拥有的名称。确保您在IB中为segue指定的名称与您在if语句中检查的名称相匹配。在此示例中,labelString是您在嵌入式控制器中设置的字符串属性。然后在该控制器的viewDidLoad方法中,您可以使用该字符串设置标签的值。

#2


5  

This is pretty much the same answer as the one by rdelmar only in Swift.

这与rdelmar仅在Swift中的答案几乎相同。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let embeddedVC = segue.destinationViewController as? MyEmbeddedController where segue.identifier == "EmbedSegue" {
        embeddedVC.labelString = self.stringToPass
    }
}

"EmbedSegue" has to the segue identifier you set in Interface Builder.

“EmbedSegue”必须具有您在Interface Builder中设置的segue标识符。

#3


0  

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];
    }
}

I should also mention that because you are using a Container view, prepareForSegue will be triggered when you'll present the ViewController that holds the Container.

我还要提一下,因为您正在使用Container视图,所以当您呈现包含Container的ViewController时,将触发prepareForSegue。

#4


0  

Answer for Swift 4:

Swift 4的答案:

if let controller = segue.destinationController as? MyEmbeddedController, segue.identifier!.rawValue == "EmbedSegue" {
    controller.labelString = self.stringToPass
}