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

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

问题描述

我在 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 传递到容器视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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