博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值
阅读量:5142 次
发布时间:2019-06-13

本文共 3300 字,大约阅读时间需要 11 分钟。

一、第一个界面

//  Created by 秦志伟 on 14-6-13.import UIKitclass ZWRootViewController: UIViewController {  init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)  // Custom initialization } var myLabel:UILabel? override func viewDidLoad() { super.viewDidLoad() var item = UIBarButtonItem(title:"下一页",style:UIBarButtonItemStyle.Plain,target:self,action:"nextBtnClicked") self.navigationItem.rightBarButtonItem = item myLabel = UILabel(frame:CGRectMake(0,100,320,50)) myLabel!.text = "Closure" myLabel!.textAlignment = NSTextAlignment.Center self.view.addSubview(myLabel!) // Do any additional setup after loading the view. } func someFunctionThatTakesAClosure(string:String) -> Void { // function body goes here myLabel!.text = string } func nextBtnClicked(){ let second = ZWSecondViewController(nibName:nil,bundle:nil) //将当前someFunctionThatTakesAClosure函数指针传到第二个界面,第二个界面的闭包拿到该函数指针后会进行回调该函数 second.initWithClosure(someFunctionThatTakesAClosure) self.navigationController.pushViewController(second,animated:true) } override func viewWillDisappear(animated: Bool){ myLabel!.hidden = true } override func viewWillAppear(animated: Bool){ myLabel!.hidden = false } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ }

二、第二个界面

//  Created by 秦志伟 on 14-6-13.import UIKit//类似于OC中的typedeftypealias sendValueClosure=(string:String)->Voidclass ZWSecondViewController: UIViewController {  var i:Int? //声明一个闭包 var myClosure:sendValueClosure? //下面这个方法需要传入上个界面的someFunctionThatTakesAClosure函数指针 func initWithClosure(closure:sendValueClosure?){ //将函数指针赋值给myClosure闭包,该闭包中涵盖了someFunctionThatTakesAClosure函数中的局部变量等的引用 myClosure = closure } init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) // Custom initialization } override func viewDidLoad() { super.viewDidLoad() i = 0 var btn = UIButton.buttonWithType(UIButtonType.System) as?UIButton btn!.frame = CGRectMake(0,100,320,50) btn!.setTitle("点击我" ,forState:UIControlState.Normal) btn!.addTarget(self,action:"action", forControlEvents:UIControlEvents.TouchUpInside) self.view.addSubview(btn) // Do any additional setup after loading the view. } func action(){ i = i!+1 //判空 if myClosure{ //闭包隐式调用someFunctionThatTakesAClosure函数:回调。 myClosure!(string: "好好哦\(i)") } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ }

转载于:https://www.cnblogs.com/Free-Thinker/p/4995739.html

你可能感兴趣的文章
笔记:创建对象的Builder模式
查看>>
如何保存putty上的各项设置
查看>>
SQL SERVER 日期sql
查看>>
JavaScript 事件流、事件处理程序及事件对象总结
查看>>
正则表达式
查看>>
canvas svg webgl threejs d3js 的区别
查看>>
现代编译原理--第三章(抽象语法树以及源码)
查看>>
MySQL用户添加和分配权限
查看>>
HDU2546-饭卡
查看>>
wind7 64 setup appjs
查看>>
hashMap源码学习记录
查看>>
Swift,字典
查看>>
NodeJs通过async/await处理异步
查看>>
Beta 冲刺(5/7)
查看>>
网页编码
查看>>
术语抽取的程序(计算机专业术语的抽取 )java代写
查看>>
SpringMVC(九) RequestMapping请求参数
查看>>
线程简介
查看>>
我的算法学习之路
查看>>
机器学习中的相似性度量
查看>>