如何将数据从ViewController传递到Container视图? [英] How do I pass data from my ViewController to a Container View?

查看:128
本文介绍了如何将数据从ViewController传递到Container视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

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

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

谢谢

推荐答案

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

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].

编辑后:

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

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

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.

这篇关于如何将数据从ViewController传递到Container视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆