swfit-block反向传值

//  ViewController.swift
//  Block

import UIKit

class ViewController: UIViewController {

    
    var myLabel = UILabel()
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.whiteColor()
        let btn = UIButton()
        btn.frame = CGRectMake(100, 300, 320, 50)
        btn.backgroundColor = UIColor.cyanColor()
        btn.setTitle("下一页", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: #selector(ViewController.nextBtnClicked), forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(btn)
        myLabel.frame = CGRectMake(0, 100, 320, 50)
        myLabel.text = "Closure"
        myLabel.textAlignment = NSTextAlignment.Center
        self.view.addSubview(myLabel)
    }
    
    func someFunctionThatTakesAClosure(string:String) -> Void {
        
        myLabel.text = string
    }
    
    func nextBtnClicked(){
        
        let second = SecondViewController()
        //将当前someFunctionThatTakesAClosure函数指针传到第二个界面,第二个界面的闭包拿到该函数指针后会进行回调该函数
        second.initWithClosure(someFunctionThatTakesAClosure)
        second.BtnName = "点我点我点我"
        self.presentViewController(second, animated: true) {
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
//  SecondViewController.swift
//  Block
import UIKit
typealias sendValueClosure=(string:String)->Void

class SecondViewController: UIViewController {


    var BtnName : String = String()
    var i:Int?
    //声明一个闭包
    var myClosure:sendValueClosure?
    //下面这个方法需要传入上个界面的someFunctionThatTakesAClosure函数指针
    func initWithClosure(closure:sendValueClosure?){
        //将函数指针赋值给myClosure闭包,该闭包中涵盖了someFunctionThatTakesAClosure函数中的局部变量等的引用
        myClosure = closure
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.whiteColor()
        i = 0
        let btn = UIButton()
        btn.backgroundColor = UIColor.cyanColor()
        btn.frame = CGRectMake(0,100,320,50)
        let string : String = "点击我"
        if BtnName.characters.count == 0 {
            BtnName = string
        }
        btn.setTitle(BtnName ,forState:UIControlState.Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        btn.addTarget(self,action:#selector(SecondViewController.action), forControlEvents:UIControlEvents.TouchUpInside)
        self.view.addSubview(btn)
    
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func action(){
        i = i!+1
        //判空
        if (myClosure != nil){
            //闭包隐式调用someFunctionThatTakesAClosure函数:回调。
            myClosure!(string: "好好哦(i)")
        }
        self.dismissViewControllerAnimated(true, completion: nil)
    }

}
原文地址:https://www.cnblogs.com/sayimba/p/5779983.html