我在试图通过容器 View 在一个屏幕上显示的两个 View Controller 之间传递数据时遇到了麻烦。
下面的最小示例 - 顶 View (TopVC) 有一个 textInput 字段。当我按下按钮时,我希望底部 View (BottomVC) 上的标签显示输入的文本。此外,我希望它向 TopVC 传回一条消息,并使用消息“成功联系底部 VC”更新 topVC 标签
Storyboard设置
我现在根本不知道如何相互引用 View Controller 。
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")
}
}
最佳答案
创建委托(delegate)
首先为您的两个容器 ViewController 创建委托(delegate)。不要忘记添加 : class
。如果您不这样做,您将无法创建弱委托(delegate)变量:
protocol TopViewControllerDelegate: class {
func sendMessage(_ string: String)
}
protocol BottomViewControllerDelegate: class {
func sendMessage(_ string: String)
}
现在为每个容器 ViewController 创建弱委托(delegate)变量
class TopViewController: UIViewController {
weak var delegate: TopViewControllerDelegate?
...
}
class BottomViewController: UIViewController {
weak var delegate: BottomViewControllerDelegate?
...
}
然后为 TopVC 实现 Bottom 的委托(delegate)和 BottomVC Top 的委托(delegate)。
extension TopViewController: BottomViewControllerDelegate {
func sendMessage(_ string: String) {
// do something
}
}
extension BottomViewController: TopViewControllerDelegate {
func sendMessage(_ string: String) {
// do something
}
}
分配代表
主 ViewController 和容器之间的 segues 应该有自己的标识符:EmbedTop
、EmbedBottom
。
因此,在您的 WrapperViewController
中为您的 Top 和 Bottom 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 的委托(delegate)设置为 BottomVC,将 BottomVC 的委托(delegate)设置为 TopVC
override func viewDidAppear(_ animated: Bool) {
topVC.delegate = bottomVC
bottomVC.delegate = topVC
}
现在您的两个 ViewController 可以相互对话了! :-)
示例:
class BottomViewController: UIViewController {
...
func speakToTop() {
delegate?.sendMessage("Hi Top!")
}
}
关于ios - 在两个同时显示的 View Controller 之间传递数据的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53834657/