【问题标题】:What's the easiest way to pass data between two simultaneously displayed view controllers?在两个同时显示的视图控制器之间传递数据的最简单方法是什么?
【发布时间】:2025-12-29 08:25:06
【问题描述】:

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

下面的最小示例 - 顶视图 (TopVC) 有一个 textInput 字段。当我按下按钮时,我希望底部视图(BottomVC)上的标签显示输入的文本。此外,我希望它向 TopVC 传递一条消息,并使用消息“成功联系底部 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) - 但显然我需要添加一些额外的代码来'介绍'这两个 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")
    }
}

【问题讨论】:

标签: ios swift swift3 uiviewcontroller closures


【解决方案1】:

创建委托

首先为您的两个容器 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 和容器之间的 segue 应该有自己的标识符: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!")
    }
}

【讨论】:

  • 不错且惯用的答案 ;) 我只需在每个 delegate 属性声明中添加一个 weak 限定符,以避免不必要的保留周期(在嵌套视图控制器之间)。
  • @PauloMattos 谢谢,我还年轻,还是个初学者,所以我总是很高兴收到这样的回复!如果我做了delegateproperitesweak,那么我必须在协议声明中添加: class,对吧?
  • 是的,很好! only 弱属性对引用类型有意义(并且将class 添加到您的协议声明中可以确保这一点)。
  • @PauloMattos。将类添加到协议声明使其成为引用类型,以便可以应用弱?
  • 谢谢!注意终端昏暗(像我一样:确保情节提要中的 segues 标识符与您在代码中调用的标识符匹配!!!)
最近更新 更多