问题描述
我正在为iPad Pro开发一个应用程序.在此应用中,containerView
用于添加其他views
并与其交互.
I'm developing an app for iPad Pro. In this app, containerView
use to add additional views
and interact with them.
首先,我创建了一个协议:
First, I created a protocol:
protocol DataViewDelegate {
func setTouch(touch: Bool)
}
然后,我创建了我的第一个视图控制器
Then, I created my first view controller
import UIKit
class ViewController: UIViewController, DataViewDelegate {
@IBOutlet var container: UIView!
@IBOutlet var labelText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
func setTouch(touch: Bool) {
if touch == true {
labelText.text = "Touch!"
}
}
}
最后,我创建了一个将嵌入到containerView中的视图.
And finally, I created a view that will be embedded in containerView.
import UIKit
class ContainerViewController: UIViewController {
var dataViewDelegate: DataViewDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func touchMe(sender: AnyObject) {
dataViewDelegate?. setTouch(true)
}
}
但是由于某种原因,什么也没发生,第一个视图控制器在setTouch函数中什么也没收到.
But for some reason, nothing happened, the first view controller receives nothing in setTouch function.
我的问题是:在这种情况下,如何使用容器来实现两个ViewsController之间的通信?
推荐答案
看起来像您定义了委托,但尚未设置委托.这一直在我身上发生.
Looks like you defined the delegate, but have not set the delegate. This happens to me all the time.
这篇关于在Swift中使用Container View进行委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!