swift 闭包(block)的传值

######A控制器

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let btn1 = UIButton(frame: CGRect(x: 0, y: 300, width: 100, height: 100))
        btn1.setTitle("push3", for: UIControlState.normal)
        btn1.backgroundColor = UIColor.orange
        self.view.addSubview(btn1)
        btn1.addTarget(self, action: #selector(pushVC3), for: UIControlEvents.touchUpInside)
        
    }

    @objc func pushVC3() {
        
        let vc = ThridViewController()
        /// 成功选择后将数据回调,并推出视图
        vc.backLocationString = { (address,province,city,area) in
            
            print(address,province,city,area)
        }
        self.navigationController?.pushViewController(vc, animated: true)
        
        
    }
######B控制器
lass ThridViewController: UIViewController {

    /// 返回数据回调
    public var backLocationString: ((String,String,String,String)->())?
    
    override func viewDidLoad() {
        super.viewDidLoad()

       self.title = "ThridViewController"
        
        let btn = UIButton(frame: CGRect(x: 0, y: 100, width: 100, height: 100));
        btn.backgroundColor = UIColor.red;
        self.view.addSubview(btn)
        btn.addTarget(self, action:#selector(popclick), for: UIControlEvents.touchUpInside)
        
        
    }

    @objc func popclick() {
       
        if backLocationString != nil{
            backLocationString!("aaa","bbb","ccc","ddd")
        }
        
    }
}

你可能感兴趣的:(swift 闭包(block)的传值)