Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将数据从视图控制器传递到容器视图?_Ios_Swift_Delegates_Containers_Segue - Fatal编程技术网

Ios 如何将数据从视图控制器传递到容器视图?

Ios 如何将数据从视图控制器传递到容器视图?,ios,swift,delegates,containers,segue,Ios,Swift,Delegates,Containers,Segue,我试图将数据从ViewController传递到其中的容器。当我按下按钮时,代理将数据发送到容器,但容器会调整大小。我怎样才能阻止这一切的发生。 我正在考虑一个prepareForSegue函数,但我不知道如何实现它,但我不知道这是否是一个解决方案 protocol VCDelegate { func passData(theData1:String) } class ViewController: UIViewController { var delegate : VC

我试图将数据从
ViewController
传递到其中的容器。当我按下按钮时,代理将数据发送到容器,但容器会调整大小。我怎样才能阻止这一切的发生。 我正在考虑一个
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) {

    }

}

您正在实例化子视图控制器的第二个新实例。但是如果您在IB中创建了一个“容器”,那么它已经为您实例化了

父视图控制器有两种将数据传递给子视图的方法。您可以在
准备(发件人:)
中传递初始数据:

如果您想稍后更新它,您可以获取相关的
子项

@IBAction func didTapButton(_ sender: Any) {
    for child in children {
        if let child = child as? SecondViewControllerProtocol {
            child.passData(string: "Updated value")
        }
    }
}
(如果需要,您显然也可以保存在
准备(for:sender:)
过程中捕获的引用。)

然后,第二个视图控制器可以相应地更新其标签:

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)
    }
}
这将产生:


谢谢您的解释。我真的很感激。这工作做得很好!!
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)
    }
}