我试图将数据从模态ViewController传递到他的源ViewController.我想我必须使用授权,但不行.
protocol communicationControllerCamera{ func backFromCamera() } class Camera: UIViewController{ var delegate: communicationControllerCamera init(){ self.delegate.backFromCamera() } } class SceneBuilder: UIViewController,communicationControllerCamera{ func backFromCamera(){ // Never called println("YEAHH") } }
解决方法
你没有设置一个代理,所以当你尝试回调FromCamera()时它是空的.
这是一个简单的工作示例,您可以测试.请注意使用可选类型(?)作为委托.
// 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’s Swift documentation找到所需的所有信息.