美文网首页
Swift Closures传值

Swift Closures传值

作者: 申申申申申 | 来源:发表于2017-07-05 12:59 被阅读6次

场景: cell 中 button 点击的时候使用 闭包, 将点击事件 传到 对应的 vc

方法1:

  1. cell 中:
class MySignCell: UICollectionViewCell {

    typealias ButtonClickBlock = (Int)->Void

    var didClickButtonBlock: ButtonClickBlock?

    @IBAction func remindButtonClick(_ sender: UIButton) {

        if (didClickButtonBlock != nil) {

            didClickButtonBlock(sender.tag)
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()

    }
}
  1. 对应的 vc 中:
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let mysign_cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MySignCell", for: indexPath) as! MySignCell

        mysign_cell.didClickButtonBlock = { (button_tag) in

            print(button_tag)
        }

        return mysign_cell
    }

方法2:

  1. cell 中:
class MySignCell: UICollectionViewCell {

    var addBtnClick:(_ sender:UIButton) -> () = { _ in }

    @IBAction func remindButtonClick(_ sender: UIButton) {

        addBtnClick(sender)
    }

    override func awakeFromNib() {
        super.awakeFromNib()

    }
}
  1. 对应的 vc 中:
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let mysign_cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MySignCell", for: indexPath) as! MySignCell

        mysign_cell.addBtnClick = { sender in

            print (sender.tag)
        }

        return mysign_cell
    }

相关文章

  • Swift Closures传值

    场景: cell 中 button 点击的时候使用 闭包, 将点击事件 传到 对应的 vc 方法1: cell 中...

  • Swift:基础(十六)闭包

    Swift 闭包 闭包(Closures)是自包含的功能代码块,可以在代码中使用或者用来作为参数传值。 Swift...

  • iOS swift 学习(二)

    Swift 闭包闭包(Closures)是自包含的功能代码块,可以在代码中使用或者用来作为参数传值。Swift 中...

  • Swift基础 : 闭包

    Swift 闭包 闭包(Closures)是包含功能的代码块, 可以在代码中使用或者用来作为参数传值 闭包的定义:...

  • Swift 闭包

    闭包(Closures)是自包含的功能代码块,可以在代码中使用或者用来作为参数传值。 Swift 中的闭包与 C ...

  • Swift中的闭包

    一、简介 闭包(Closures)是自包含的功能代码块,可以在代码中使用或者用来作为参数传值。Swift 中的闭包...

  • Swift 闭包

    闭包(Closures)是自包含的功能代码块,可以在代码中使用或者用来作为参数传值。Swift 中的闭包与 C 和...

  • Swift -- 闭包

    1.闭包的概念:闭包(Closures)是自包括的功能代码块,能够在代码中使用或者用来作为参数传值。在Swift中...

  • Swift - 闭包

    闭包(Closures)是自包含的功能代码块,可以在代码中使用或者用来作为参数传值。Swift中的闭包与C语言和O...

  • OC与swift的数据传输

    简介 该项目主要介绍了oc与swift之间、swift内部几种常见的传值方式(属性传值、代码块传值、代理传值、通知...

网友评论

      本文标题:Swift Closures传值

      本文链接:https://www.haomeiwen.com/subject/gufqhxtx.html