Swift 正向传值以及利用闭包(closure)实现反向传值(七)

Swift 正向传值以及利用闭包(closure)实现反向传值(七),第1张

概述直接上代码吧 - - Demo地址 https://github.com/Zhangjingwang1993/closureDemo.git // MainVclet button = UIButton.init(type: UIButtonType.Custom)button.frame = CGRectMake(20, 100, 50, 50);button.backgroundColo

直接上代码吧 - -
Demo地址
https://github.com/Zhangjingwang1993/closureDemo.git

// MainVclet button = UIbutton.init(type: UIbuttonType.Custom)button.frame = CGRectMake(20,100,50,50);button.backgroundcolor = UIcolor.cyancolor()self.vIEw.addSubvIEw(button)button.addTarget(self,action: Selector("click:"),forControlEvents: UIControlEvents.touchUpInsIDe)textFIEld = UITextFIEld.init(frame: CGRectMake(20,160,50));self.vIEw.addSubvIEw(textFIEld)textFIEld.placeholder = "I am Placehoder"
func addressthatTakesAClosure(string:String) ->VoID{        textFIEld.text = string    }
func click(sender: UIbutton)    {        // 初始化        let sec = MainVIEwController()        // 类似于属性传值        sec.string = "Success"        /***********************************************/        // ---------------华丽的分割线---------------- //        // 用函数把地址传过去,用于回调        // sec.initWithClosure(addressthatTakesAClosure)        // 或者直接这样        sec.myClosure = addressthatTakesAClosure        self.navigationController?.pushVIEwController(sec,animated: true)    }
// SecVc// 类似于OC中的typedef    typealias sendValueClosure = (string:String)->VoID
var string = ""    // 声明一个Closure(闭包)    var myClosure:sendValueClosure?    // 下面这个方法需要传入上个界面的addressthatTakesAClosure函数指针    func initWithClosure(closure:sendValueClosure?){        myClosure = closure    }
// 类似于OC中的属性传值    print("Success: \(string)")    let label = UILabel.init(frame: CGRectMake(10,150,30))    label.text = string    self.vIEw.addSubvIEw(label)
func backbuttonCreate()    {        // 返回按钮     var buttonRight = UIbutton()     buttonRight = UIbutton.init(type: UIbuttonType.Custom)     buttonRight.backgroundcolor = UIcolor.redcolor()     buttonRight.frame = CGRectMake(280,50)     self.vIEw.addSubvIEw(buttonRight)     buttonRight.setTitle("返回",forState: UIControlState.normal)     buttonRight.addTarget(self,action: Selector("click"),forControlEvents: UIControlEvents.touchUpInsIDe)    }
// 返回点击方法func click()    {        if myClosure != nil{            myClosure!(string: string);        }                                                    self.navigationController?.popToRootVIEwControllerAnimated(true)    }
总结

以上是内存溢出为你收集整理的Swift 正向传值以及利用闭包(closure)实现反向传值(七)全部内容,希望文章能够帮你解决Swift 正向传值以及利用闭包(closure)实现反向传值(七)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1078557.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存