简体   繁体   English

如何将数据从View Controller传递到Container View?

[英]How would I pass data from a View Controller to a Container View?

在此处输入图片说明

I'm trying to pass data from a ViewController to a Container within it. 我正在尝试将数据从ViewController传递到其中的Container。 When I press the button the delegate sends the data to the container but the container resizes. 当我按下按钮时,委托将数据发送到容器,但是容器会调整大小。 How would I stop this from happening. 我将如何阻止这种情况的发生。 I'm thinking a prepareForSegue function but I can't figure out how to implement it but I don't know if even that would be a solution. 我在想一个prepareForSegue函数,但我不知道如何实现它,但我不知道这是否是解决方案。

protocol VCDelegate {

    func passData(theData1:String)

}

class ViewController: UIViewController {

    var delegate : VCDelegate?

    @IBAction func sendTextToContainer(_ sender: Any) {

        let ViewC = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController

        let ContainerV = self.storyboard!.instantiateViewController(withIdentifier: "ContainerView") as! ContainerView

        self.present(ContainerV,animated:true,completion: nil)

        ViewC.delegate = ContainerV

        ViewC.delegate?.passData(theData1:"Hello")
    } 
}

class ContainerView: UIViewController, VCDelegate {

    func passData(theData1: String) {

        print(theData1)
        theText.text = theData1

    }

    @IBOutlet weak var theText: UILabel!

    override func viewWillAppear(_ animated: Bool) {

    }

}

You are instantiating a new, second instance of the child view controller. 您正在实例化子视图控制器的第二个新实例。 But if you created a “container” in IB, it's already been instantiated for you. 但是,如果您在IB中创建了一个“容器”,则已经为您实例化了。

There are two ways for the parent view controller to pass data to the child. 父视图控制器有两种方式将数据传递给子视图。 You can pass the initial data in prepare(for:sender:) : 您可以在prepare(for:sender:)传递初始数据:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? SecondViewControllerProtocol {
        destination.passData(string: "Initial value")
    }
}

If you want to later update it, you can grab the relevant children : 如果以后要更新,可以抓住相关的children

@IBAction func didTapButton(_ sender: Any) {
    for child in children {
        if let child = child as? SecondViewControllerProtocol {
            child.passData(string: "Updated value")
        }
    }
}

(You obviously could save the reference you captured during prepare(for:sender:) , too, if you wanted.) (显然,您也可以保存在prepare(for:sender:)期间捕获的引用。)

The second view controller can then update its label accordingly: 然后,第二个视图控制器可以相应地更新其标签:

protocol SecondViewControllerProtocol {
    func passData(string: String) 
}

class SecondViewController: UIViewController {
    @IBOutlet weak var label: UILabel!

    private var string: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        // this is in case you passed the data before the label was hooked up

        label.text = string
    }
}

extension SecondViewController: SecondViewControllerProtocol {
    func passData(string: String) {
        self.string = string

        guard let label = label else { return }

        UIView.transition(with: label, duration: 0.25, options: .transitionCrossDissolve, animations: {
            label.text = string
        }, completion: nil)
    }
}

That yields: 产生:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从View控制器传递到Container? - How to pass data from View controller to Container? 如何将数据从视图 Controller 传递到 swift 中的容器视图 - How to pass data from View Controller to Container View in swift 当数据从一个视图 Controller 传递到另一个视图时,我如何能够传递图像? - How would I be able to pass an image when data is passed from one View Controller to another? 在容器视图中将数据传递到控制器 - Pass data to controller in container view 如何将数据从父视图控制器传递到子容器视图控制器 - How to pass data from parent view controller to child container view controller 如何在容器视图和主视图控制器之间正确传递数据 - How to pass data between container view and main view controller properly 如何将数据传递到容器视图中的视图控制器? - How to pass data to a view controller within an container view? 如何将数据从ViewController传递到Container View? - How do I pass data from my ViewController to a Container View? 如何将视图 controller 中的数据传递到表视图 controller? - How to pass the data from view controller to the table view controller? 如何将标签中的数据从表视图行操作发送到视图控制器? - How would I send data from labels from a table view row action to a view controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM