swift 闭包

step1:在修改值的VC创建闭包

    //给闭包起一个别名
    typealias ChangeTextAction = (String) -> ()
    //闭包属性
    var changeBlock: ChangeTextAction?

setp2:在传值VC把新值传入闭包

    ///判断闭包是否为空
    guard let changeBlock = changeBlock else {
        return
    }
    //传值
    changeBlock(inputValue)
    self.dismiss(animated: true, completion: nil)

step3:使用值的VC使用值

    let blockVC = BlockViewController.init()
    blockVC.labelText = newLabel.text
        blockVC.changeBlock = { newStr in
        newLabel.text = newStr //替换新值
    }
    self.present(blockVC, animated: true, completion: nil)

你可能感兴趣的:(ios,ios学习笔记)