我陷入了混乱,试图通过容器视图在一个屏幕上显示的两个视图控制器之间传递数据。
下面的最小示例-顶视图(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")
}
}
答案 0 :(得分:2)
首先为两个容器ViewController创建委托。不要忘记添加: class
。如果不这样做,将无法创建弱委托变量:
protocol TopViewControllerDelegate: class {
func sendMessage(_ string: String)
}
protocol BottomViewControllerDelegate: class {
func sendMessage(_ string: String)
}
现在,为每个ViewController容器创建弱委托变量
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和容器之间的任务应具有自己的标识符:EmbedTop
,EmbedBottom
。
因此,在您的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!")
}
}