用闭包在两个界面传递数据

ViewController

@IBAction func didClicked(sender: AnyObject) {
        let secondCtrl = SecondViewController()
        secondCtrl.handleClick = {
            print("点击了第二个页面", $0)
        }
        
        secondCtrl.fetchData = {
            return "第一个页面的数据"
        }
        self.presentViewController(secondCtrl, animated: true, completion: nil)
    }

SecondViewController

class SecondViewController: UIViewController {
    var handleClick: ((data: String) -> Void)?
    var fetchData: (() -> String)?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if let fetch = fetchData {
            let s = fetch()
            print("从第一个页面到第二个", s)
        }

        self.view.backgroundColor = UIColor.purpleColor()
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        //触摸开始
        if let click = handleClick {
            click(data: "从第二个到第一个")
        }
        
        self.dismissViewControllerAnimated(true, completion: nil)
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容