Vykintas
Vykintas

Reputation: 399

Swift container pass data

I have problem with container in swift

I need know when user click button in Controller like photos: enter image description here

how to know when button is clicked in Container?

Upvotes: 0

Views: 125

Answers (1)

vacawama
vacawama

Reputation: 154691

In order to call a method in the ContainedViewController, you will first need to get a reference to it. An easy way to do this is to click on the Embed segue in the Document Outline view and give this segue an identifier such as "embedContainedVC".

Document Outline View


Name embedded segue

Then you can use that identifier in prepareForSegue to get a reference to the embedded view controller and use that to call a method on that view controller when the button is clicked.

ViewController.swift:

class ViewController: UIViewController {

    @IBOutlet weak var container: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    var containedVC: ContainedViewController!

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "embedContainedVC" {
            containedVC = segue.destinationViewController as! ContainedViewController
        }
    }

    @IBAction func doClick(sender: AnyObject) {
        print("call contained VC")
        containedVC.clickedClick()
    }
}

ContainedViewController.swift:

import UIKit

class ContainedViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func clickedClick() {
        print("click")
    }
}

Upvotes: 2

Related Questions