Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值


利用了大约一个多小时来搞明白OC中Blocks反向传值和Swift中Closure反向传值的差别,下面直接贴上代码:

一、第一个界面

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //  Created by 秦志伟 on 14-6-13.  
  2. import UIKit  
  3.   
  4. class ZWRootViewController: UIViewController {  
  5.   
  6.     init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {  
  7.         super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)  
  8.         // Custom initialization  
  9.     }  
  10.     var myLabel:UILabel?  
  11.     override func viewDidLoad() {  
  12.         super.viewDidLoad()  
  13.           
  14.         var item = UIBarButtonItem(title:"下一页",style:UIBarButtonItemStyle.Plain,target:self,action:"nextBtnClicked")  
  15.         self.navigationItem.rightBarButtonItem = item  
  16.           
  17.           
  18.         myLabel = UILabel(frame:CGRectMake(0,100,320,50))  
  19.         myLabel!.text = "Closure"  
  20.         myLabel!.textAlignment = NSTextAlignment.Center  
  21.         self.view.addSubview(myLabel!)  
  22.         // Do any additional setup after loading the view.  
  23.     }  
  24.     func someFunctionThatTakesAClosure(string:String) -> Void {  
  25.         // function body goes here  
  26.         myLabel!.text = string  
  27.     }  
  28.     func nextBtnClicked(){  
  29.         let second = ZWSecondViewController(nibName:nil,bundle:nil)  
  30.         //将当前someFunctionThatTakesAClosure函数指针传到第二个界面,第二个界面的闭包拿到该函数指针后会进行回调该函数  
  31.         second.initWithClosure(someFunctionThatTakesAClosure)  
  32.         self.navigationController.pushViewController(second,animated:true)  
  33.           
  34.     }  
  35.       
  36.     override func viewWillDisappear(animated: Bool){  
  37.         myLabel!.hidden = true  
  38.     }  
  39.     override func viewWillAppear(animated: Bool){  
  40.         myLabel!.hidden = false  
  41.     }  
  42.     override func didReceiveMemoryWarning() {  
  43.         super.didReceiveMemoryWarning()  
  44.         // Dispose of any resources that can be recreated.  
  45.     }  
  46.       
  47.   
  48.     /* 
  49.     // #pragma mark - Navigation 
  50.  
  51.     // In a storyboard-based application, you will often want to do a little preparation before navigation 
  52.     override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) { 
  53.         // Get the new view controller using [segue destinationViewController]. 
  54.         // Pass the selected object to the new view controller. 
  55.     } 
  56.     */  
  57.   
  58. }  

二、第二个界面

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //  Created by 秦志伟 on 14-6-13.  
  2. import UIKit  
  3. //类似于OC中的typedef  
  4. typealias sendValueClosure=(string:String)->Void  
  5. class ZWSecondViewController: UIViewController {  
  6.     var i:Int?  
  7.     //声明一个闭包  
  8.     var myClosure:sendValueClosure?  
  9.     //下面这个方法需要传入上个界面的someFunctionThatTakesAClosure函数指针  
  10.     func initWithClosure(closure:sendValueClosure?){  
  11.         //将函数指针赋值给myClosure闭包,该闭包中涵盖了someFunctionThatTakesAClosure函数中的局部变量等的引用  
  12.         myClosure = closure  
  13.     }  
  14.       
  15.     init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {  
  16.         super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)  
  17.           
  18.         // Custom initialization  
  19.     }  
  20.   
  21.     override func viewDidLoad() {  
  22.         super.viewDidLoad()  
  23.         i = 0  
  24.         var btn = UIButton.buttonWithType(UIButtonType.System) as?UIButton  
  25.         btn!.frame = CGRectMake(0,100,320,50)  
  26.         btn!.setTitle("点击我" ,forState:UIControlState.Normal)  
  27.         btn!.addTarget(self,action:"action", forControlEvents:UIControlEvents.TouchUpInside)  
  28.         self.view.addSubview(btn)  
  29.           
  30.         // Do any additional setup after loading the view.  
  31.     }  
  32.     func action(){  
  33.         i = i!+1  
  34.         //判空  
  35.         if myClosure{  
  36.             //闭包隐式调用someFunctionThatTakesAClosure函数:回调。  
  37.             myClosure!(string: "好好哦\(i)")  
  38.         }  
  39.     }  
  40.     override func didReceiveMemoryWarning() {  
  41.         super.didReceiveMemoryWarning()  
  42.         // Dispose of any resources that can be recreated.  
  43.     }  
  44.       
  45.   
  46.     /* 
  47.     // #pragma mark - Navigation 
  48.  
  49.     // In a storyboard-based application, you will often want to do a little preparation before navigation 
  50.     override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) { 
  51.         // Get the new view controller using [segue destinationViewController]. 
  52.         // Pass the selected object to the new view controller. 
  53.     } 
  54.     */  
  55.   
  56. }  
http://blog.csdn.net/zhiwei_qin/article/details/30510577?utm_source=tuicool&utm_medium=referral

转载请注明!!!,欢迎加入iOS交流群: 爱疯、爱Coding:209476515
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值