快速访问容器视图子属性 [英] Access container view child properties swift

查看:75
本文介绍了快速访问容器视图子属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现的目标:

用户按下ViewController中的按钮,然后,放置在容器视图中的按钮的颜色应更改为红色。

User presses the button in the ViewController then, the color of the button placed in the container view should change its color to red.

如何从ViewController中访问放置在容器视图中的按钮?

How can I get access of the button placed in the container view, from the ViewController?

推荐答案

逐步操作:


  1. 命名视图控制器和容器视图控制器之间的顺序。

  2. 向视图控制器添加一个属性,其中将包含

  3. 在您的视图控制器中实现方法 prepareForSegue(_:sender:)

  4. 在方法中检查 segue.identifier 是否等于您在步骤1中指定的标识符。

  5. 如果为true,则保存 segue .destinationViewController 从第2步移至您的媒体资源。

  6. 现在您已将容器视图控制器存储在媒体资源中,因此您可以从类中进行自定义。您应该已经将视图控制器存储在 viewDidLoad()方法中。

  1. Name the segue between your view controller and container view controller.
  2. Add a property to your view controller which will contain the container view controller.
  3. In your view controller implement a method prepareForSegue(_:sender:).
  4. In the method check if segue.identifier equals the identifier you specified in step 1.
  5. If true, then save the segue.destinationViewController to your property from step 2.
  6. Now you have the container view controller stored in your property so you can do customization from your class. You should have the view controller stored in viewDidLoad() method already.

示例:

var containerViewController: YourContainerViewControllerClass?
let containerSegueName = "testSegue"
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == containerSegueName {
        containerViewController = segue.destinationViewController as? YourContainerViewControllerClass
    }
}

这篇关于快速访问容器视图子属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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