美文网首页
swift 闭包的使用

swift 闭包的使用

作者: BrumeLoong | 来源:发表于2018-01-01 13:02 被阅读0次

    闭包的使用方式和场景很多,这里我就用闭包传值来做例子吧
    这里我两边都用了闭包来传值,也使用了两种方法,一种是属性闭包传值,另外一个是方法闭包传值

    class BlockTViewController: UIViewController {
        
        var isBloc : ((_ str : String)->Void)?
        var label : UITextView?
        var swiftB : SwiftBlockViewController?
        override func viewDidLoad() {
            super.viewDidLoad()
            label = UITextView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
            swiftB = SwiftBlockViewController()
            swiftB?.goTwoBlock = {
                (str) in
                self.label?.text = str
            }
            label?.layer.borderWidth = 1
            view.addSubview(label!)
            // Do any additional setup after loading the view.
            view.backgroundColor = UIColor.green
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        
        func blockMethod(str : String , methodBlock : (_ str : String)-> Void){
            print(str)
            methodBlock("orewa tsuna")
        }
        
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    //        self.present(swiftB!, animated: true, completion: nil)
            self.dismiss(animated: true, completion: nil)
            isBloc!("属性闭包")
        }
    
    
    class SwiftBlockViewController: UIViewController{
    
        var swiftT = BlockTViewController()
        var label : UITextView?
        var goTwoBlock : ((_ str : String)->Void)?
        override func viewDidLoad() {
            super.viewDidLoad()
    //        swiftT = BlockTViewController()
            // Do any additional setup after loading the view.
            label = UITextView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
            label?.layer.borderWidth = 1
            view.addSubview(label!)
            view.backgroundColor = UIColor.yellow
            swiftT.isBloc = {
                (str :String) in
                self.label?.text = str
            }
    //        swiftT.blockMethod(str: "give your value") { (test) in
    //            label?.text = test
    //        }
            
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    //        self.dismiss(animated: true, completion: nil)
    //        goTwoBlock!("ahoaho")
            
            self.present(swiftT, animated: true, completion: nil)
    //        goTwoBlock!((label?.text)!
        }
    
    
    }
    
    

    以上,就是闭包传值的一些使用情况了,要是有哪里不对的请各位指出!!!

    后记:根据我自己的使用framework包的情况,好像在打出framework包后引入其他工程并不需要做配置了,这个可能要根据个人情况来了。望悉知

    相关文章

      网友评论

          本文标题:swift 闭包的使用

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