xcode 快速访问容器视图子属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33857210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:14:32  来源:igfitidea点击:

Access container view child properties swift

xcodeswiftpropertiescontainersuicontainerview

提问by Tom el Safadi

What I want to achieve:

我想要达到的目标:

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?

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

enter image description here

在此处输入图片说明

回答by sunshinejr

Step by step:

一步步:

  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.identifierequals the identifier you specified in step 1.
  5. If true, then save the segue.destinationViewControllerto 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.
  1. 命名视图控制器和容器视图控制器之间的 segue。
  2. 向视图控制器添加一个属性,该属性将包含容器视图控制器。
  3. 在您的视图控制器中实现一个方法prepareForSegue(_:sender:)
  4. 在方法中检查是否segue.identifier等于您在步骤 1 中指定的标识符。
  5. 如果为 true,则将segue.destinationViewController第 2 步中的保存到您的属性中。
  6. 现在您已将容器视图控制器存储在您的属性中,以便您可以从您的类中进行自定义。您应该已经将视图控制器存储在viewDidLoad()方法中。

Example:

例子:

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

回答by Andrey Gordeev

I recommend not to rely on segue.identifier, but rather test for destinationtype directly:

我建议不要依赖segue.identifier,而是destination直接测试类型:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)

    if let vc = segue.destination as? YourViewController {
        vc.someVariable = true
    }
}

This way you avoid mistakes with a misspelled segue name.

这样你就可以避免拼写错误的 segue 名称的错误。

回答by Saranjith

Swift 4, Xcode 9.4.1

斯威夫特 4,Xcode 9.4.1

var contentViewController : UIContentViewController?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == containerSegueName {
       contentViewController = segue.destination as? UIContentViewController
    }
}

回答by asdf

Swift 3 for macOS:

适用于 macOS 的 Swift 3:

// MARK: - Container View Controller

var containerViewController: ContainerViewController?

let containerSegueIdentifier = "Container Segue"

override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    if segue.identifier == containerSegueIdentifier {
        if let connectContainerViewController = segue.destinationController as? FormationViewController {
            formationViewController = connectContainerViewController
        }
    }
}

Check identifier and controller class.

检查标识符和控制器类。