在两个同时显示的视图控制器之间传递数据的最简单方法是什么?

What's the easiest way to pass data between two simultaneously displayed view controllers?

我在试图通过容器视图在一个屏幕上显示的两个视图控制器之间传递数据时遇到了麻烦。

下面的最小示例 - 顶部视图 (TopVC) 有一个 textInput 字段。当我按下按钮时,我希望底部视图 (BottomVC) 上的标签显示输入的文本。此外,我希望它向 TopVC 传回一条消息并使用消息 "Successfully contacted bottom VC"

更新 topVC 标签

故事板设置

我现在基本上不知道如何相互引用视图控制器。

class TopViewController: UIViewController {

    @IBOutlet weak var textInput: UITextField!
    @IBOutlet weak var textOutput: UILabel!

    @IBAction func go(_ sender: UIButton) {
        // ??????????? 
    }

    override func viewDidLoad() {  
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.blue
    }

    func callMeBaby(greeting: String) {
        textOutput.text = greeting
    }
}

?????? 占位符中,我想放一些基本上像 BottomVC.test(textInput.text, callmebaby) 这样的东西 - 但显然我需要向 'introduce' 两个 ViewController 添加一些额外的代码,但我不确定该怎么做。

class BottomViewController: UIViewController {

    @IBOutlet weak var textLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.yellow
    }

    func test(input: String, completion: (String) -> Void) {
        textLabel.text = input
        completion("Successfully contacted bottom VC")
    }
}

创建代表

首先为您的两个容器 ViewController 创建委托。不要忘记添加 : class。如果你不这样做,你将无法创建 weak 委托变量:

protocol TopViewControllerDelegate: class {
    func sendMessage(_ string: String)
}
protocol BottomViewControllerDelegate: class {
    func sendMessage(_ string: String)
}

现在为每个容器 ViewController 创建 weak 委托变量

class TopViewController: UIViewController {
    weak var delegate: TopViewControllerDelegate?
    ...
}

class BottomViewController: UIViewController {
    weak var delegate: BottomViewControllerDelegate?
    ...
}

然后为 TopVC 实现 Bottom 的委托和 BottomVC Top 的委托。

extension TopViewController: BottomViewControllerDelegate {
    func sendMessage(_ string: String) {
        // do something
    }
}
extension BottomViewController: TopViewControllerDelegate {
    func sendMessage(_ string: String) {
        // do something
    }
}

分配代表

主要 ViewController 和容器之间的 segues 应该有自己的标识符:EmbedTopEmbedBottom

所以在你的 WrapperViewController 中为你的顶部和底部创建变量 ViewController 并覆盖方法 prepare(for:sender:) 并在内部分配这些变量

class WrapperViewController: UIViewController {

    var topVC: TopViewController?
    var bottomVC: BottomViewController?

    ...

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "EmbedTop" {
            topVC = segue.destination as! TopViewController
        } else if segue.identifier == "EmbedBottom" {
            bottomVC = segue.destination as! BottomViewController
        }
    }

}

最后在 viewDidAppear 中将 TopVC 的委托设置为 BottomVC,将 BottomVC 的委托设置为 TopVC

override func viewDidAppear(_ animated: Bool) {
    topVC.delegate = bottomVC
    bottomVC.delegate = topVC
}

现在你们两个 ViewController 可以互相说话了! :-)


示例:

class BottomViewController: UIViewController {
    ...
    func speakToTop() {
        delegate?.sendMessage("Hi Top!")
    }
}