本文介绍了在swift中解除模态viewController时传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将数据从模式ViewController 传递到他的源ViewController 。我想我必须使用委托但它不起作用。
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?
推荐答案
您没有设置委托,因此当您尝试调用<$ c时它是空的$ c> 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")
}
}
你可以找到你的所有信息需要。
You can find all the information you need in Apple's Swift documentation.
这篇关于在swift中解除模态viewController时传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!