我正在尝试将数据从 modal ViewController 传递到他的 source ViewController.我想我必须使用delegation,但它不起作用.
I'm trying to pass data from the modal ViewController to his source ViewController. I think I have to use delegation but it doesn't work.
protocol communicationControllerCamera{
func backFromCamera()
}
class Camera: UIViewController{
var delegate: communicationControllerCamera
init(){
self.delegate.backFromCamera()
}
}
class SceneBuilder: UIViewController, communicationControllerCamera{
func backFromCamera(){ // Never called
println("YEAHH")
}
}
没有调用 backFromCamera 方法.我做错了什么?
The backFromCamera method it's not called. What did I do wrong?
你没有设置代理,所以当你尝试调用 backFromCamera()
时它是空的.
You didn't set a delegate so it was empty when you tried to call backFromCamera()
.
这是一个您可以测试的简单工作示例.请注意委托的可选类型 (?) 的使用.
Here's a simple working example you can test out. Notice the use of the optional type (?) for the delegate.
// Camera class
protocol communicationControllerCamera {
func backFromCamera()
}
class Camera: UIViewController {
var delegate: communicationControllerCamera? = nil
override func viewDidLoad() {
super.viewDidLoad()
self.delegate?.backFromCamera()
}
}
// SceneBuilder class
class SceneBuilder: UIViewController, communicationControllerCamera {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
var myCamera = Camera()
myCamera.delegate = self
self.presentModalViewController(myCamera, animated: true)
}
func backFromCamera() {
println("Back from camera")
}
}
您可以在 Apple 的 Swift 文档.
这篇关于快速关闭模态视图控制器时传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!