快速关闭模态视图控制器时传递数据

Pass data when dismiss modal viewController in swift(快速关闭模态视图控制器时传递数据)
本文介绍了快速关闭模态视图控制器时传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据从 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 文档.

这篇关于快速关闭模态视图控制器时传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)