美文网首页
swift中闭包的使用

swift中闭包的使用

作者: 不简单的风度 | 来源:发表于2016-06-23 17:16 被阅读51次

swift中的闭包跟OC中的Block原理相似
本文用一个小Demo来展示具体用法
首先自定义了一个view,然后定义闭包,最后在viewcontroller中创建view并使用闭包传值

自定义view的代码:


import UIKit

//typealias IndexBlock  = (index:NSInteger)->Void

class CustomView: UIView {
    
    var IndexBlock:((index:NSInteger)->()) = {_ in }

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    
//    var callBack:IndexBlock!
    
    var button1:UIButton?
    var button2:UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        self.button1 = UIButton.init(type: .Custom)
        self.button1?.backgroundColor = UIColor.redColor()
        self.button1?.frame = CGRectMake(0, 65, 60, 30)
        self.button1?.tag = 1
        self.button1?.addTarget(self, action: #selector(buttonClick), forControlEvents: .TouchUpInside)
        self.addSubview(self.button1!)
        
        self.button2 = UIButton.init(type: .Custom)
        self.button2?.backgroundColor = UIColor.blueColor()
        self.button2?.frame = CGRectMake(0, 100, 60, 30)
        self.button2?.tag = 2
        self.button2?.addTarget(self, action: #selector(buttonClick), forControlEvents: .TouchUpInside)
        self.addSubview(self.button2!)
        
    }
    
    func buttonClick(sender:UIButton){
        
//        self.callBack!(index: sender.tag)
        self.IndexBlock(index: sender.tag)
    }
    

}

viewcontroller中的代码:


import UIKit

class ViewController: UIViewController {

    var cv:CustomView?
    
    var label:UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        configViews()
        
        
    }
    
    func configViews() {
        
        self.title = "首页"
        
        self.view.backgroundColor = UIColor.whiteColor()
        
        setupData()
        
        
    }

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

}

extension ViewController {
    
    func setupData(){
        self.cv = CustomView.init(frame: CGRectMake(10, 10, 300, 300))
        self.cv!.backgroundColor = UIColor.yellowColor()
        self.view.addSubview(self.cv!)
        
        self.label = UILabel.init(frame: CGRectMake(10, 360, 200, 30))
        self.label.backgroundColor = UIColor.redColor()
        self.view.addSubview(self.label)
        
        /*
        self.cv?.callBack = {(index:NSInteger)->() in
            self.label.text = String(index)
            }
            */
        self.cv?.IndexBlock = {(index:NSInteger)->() in
            self.label.text = String(index)

            }
 
        }
    
}

代码中闭包用了两种写法,一种是常规写法,一种使用了typealias
不过我注释掉了其中一种写法,两种实现效果是一样的,可以根据自己的习惯去选择哪种写法

相关文章

  • Swift学习笔记(1)

    SWift学习笔记 闭包 闭包表达式 闭包是自包含的函数代码块,可以在代码中被传递和使用。Swift 中的闭包与 ...

  • swift中的闭包

    swift中的闭包 闭包是自包含的函数代码块,可以在代码中被传递和使用。swift中的闭包与C和Objective...

  • swift 闭包(闭包表达式、尾随闭包、逃逸闭包、自动闭包)

    闭包是自含的函数代码块,可以在代码中被传递和使用 闭包和swift的对比 Swift 中闭包与OC的 block ...

  • Swift 难点

    关于Swift的闭包,尾随闭包,Swift 中类型检测使用关键字is,类型转换使用关键字as。Any类,和AnyC...

  • Swift-闭包

    Swift 闭包 函数 ()->() Swift 中的闭包和 Objective-C 中的 block 类似,闭包...

  • swift中GCD的使用详情

    想看swift3.0使用GCD,请点击GCD详解 想看swift3.0闭包的使用和介绍,请点击Swift版闭包使用...

  • Swift闭包和函数

    函数在Swift中只是一种特殊的闭包,闭包在Swift语言中是一等公民,支持闭包嵌套和闭包传递。Swift中的闭包...

  • 1 Swift闭包使用-类似于OC中block

    ?Swift闭包的使用

  • Swift5.5学习笔记六:闭包(Closures)

    //闭包(Closures)//闭包是独立的功能块,可以在代码中传递和使用。//Swift中的闭包类似于 C 和 ...

  • Swift学习:闭包

    本篇将详细总结介绍Swift闭包的用法;闭包是自包含的函数代码块,可以在代码中被传递和使用。Swift中的闭包与C...

网友评论

      本文标题:swift中闭包的使用

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