swift 闭包的使用

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

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包后引入其他工程并不需要做配置了,这个可能要根据个人情况来了。望悉知

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • /* 闭包: 函数是闭包的一种 类似于OC语言的block 闭包表达式(匿名函数) --能够捕获上下文中的值 语法...
    Jason_Wong阅读 586评论 1 8
  • 代理的使用方式和场景很多,这里我就用代理传值来做例子吧 以上,就是代理传值的基本一个使用,要是有哪里不对的请各位指...
    BrumeLoong阅读 538评论 0 0
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,405评论 25 708
  • 2017-10-25 仙谷健康管理咨询中心 每个人都想要健康,那你知道可以从何做起吗?没错,合理的膳食是一个重要的...
    7de21fed4386阅读 464评论 0 0
  • 《千与千寻》是一部大名鼎鼎的片子,却一直很遗憾没欣赏过。近来女儿迷上了宫崎峻,就陪她一起看了这部剧。当我分享出来观...
    愿缘圆源阅读 1,119评论 0 6