swift 闭包传值

2018-09-13  本文已影响0人  勇敢的我2017

场景:A页面跳转到B页面,B页面返回到A页面,(B页面给A页面传值)

B页面逻辑:创建block,声明变量,传值

A页面逻辑:回调

B页面代码:

1、创建block

typealias SecondViewControllerBlock = (String,Int)->Void

2、声明block 变量

 var block : SecondViewControllerBlock?

 3、传值

    @IBAction func back(_sender:UIButton) {

        ifself.block!=nil{

            self.block!(passTf.text??"",26)

        }

        self.navigationController?.popViewController(animated:true)

    }

A页面代码:

block 回调

    @IBAction func next(_sender:UIButton) {

        let secondVC =SecondViewController()

        secondVC.block= {

            (name,age)in

            self.textLabel.text=String(format:"%@%d", name,age)

        }

        self.navigationController?.pushViewController(secondVC, animated:true)

    }

上一篇 下一篇

猜你喜欢

热点阅读