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

前端之家收集整理的这篇文章主要介绍了Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

OC中Blocks反向传值和Swift中Closure反向传值的差别,下面直接贴上代码

一、第一个界面

[objc] view plain copy
  1. importUIKit
  2. @H_403_29@
  3. @H_403_29@classZWRootViewController:UIViewController{
  4. @H_403_29@init(nibNamenibNameOrNil:String?,bundlenibBundleOrNil:NSBundle?){
  5. @H_403_29@super.init(nibName:nibNameOrNil,bundle:nibBundleOrNil)
  6. @H_403_29@//Custominitialization
  7. @H_403_29@}
  8. @H_403_29@varmyLabel:UILabel?
  9. @H_403_29@overridefuncviewDidLoad(){
  10. super.viewDidLoad()
  11. @H_403_29@varitem=UIBarButtonItem(title:"下一页",style:UIBarButtonItemStyle.Plain,target:self,action:"nextBtnClicked")
  12. self.navigationItem.rightBarButtonItem=item
  13. @H_403_29@
  14. @H_403_29@myLabel=UILabel(frame:CGRectMake(0,100,320,50))
  15. @H_403_29@myLabel!.text="Closure"
  16. @H_403_29@myLabel!.textAlignment=NSTextAlignment.Center
  17. self.view.addSubview(myLabel!)
  18. //Doanyadditionalsetupafterloadingtheview.
  19. @H_403_29@funcsomeFunctionThatTakesAClosure(string:String)->Void{
  20. @H_403_29@//functionbodygoeshere
  21. @H_403_29@myLabel!.text=string
  22. @H_403_29@funcnextBtnClicked(){
  23. @H_403_29@letsecond=ZWSecondViewController(nibName:nil,bundle:nil)
  24. //将当前someFunctionThatTakesAClosure函数指针传到第二个界面,第二个界面的闭包拿到该函数指针后会进行回调该函数
  25. @H_403_29@second.initWithClosure(someFunctionThatTakesAClosure)
  26. self.navigationController.pushViewController(second,animated:true)
  27. @H_403_29@}
  28. @H_403_29@overridefuncviewWillDisappear(animated:Bool){
  29. @H_403_29@myLabel!.hidden=true
  30. @H_403_29@overridefuncviewWillAppear(animated:Bool){
  31. @H_403_29@myLabel!.hidden=false
  32. @H_403_29@overridefuncdidReceiveMemoryWarning(){
  33. super.didReceiveMemoryWarning()
  34. //DispoSEOfanyresourcesthatcanberecreated.
  35. /*
  36. //#pragmamark-Navigation
  37. //Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation
  38. overridefuncprepareForSegue(segue:UIStoryboardSegue?,sender:AnyObject?){
  39. //Getthenewviewcontrollerusing[seguedestinationViewController].
  40. //Passtheselectedobjecttothenewviewcontroller.
  41. }
  42. */
  43. @H_403_29@}

二、第二个界面

copy

//类似于OC中的typedef
  • @H_403_29@typealiassendValueClosure=(string:String)->Void
  • ZWSecondViewController:UIViewController{
  • i:Int?
  • //声明一个闭包
  • myClosure:sendValueClosure?
  • //下面这个方法需要传入上个界面的someFunctionThatTakesAClosure函数指针
  • @H_403_29@funcinitWithClosure(closure:sendValueClosure?){
  • //将函数指针赋值给myClosure闭包,该闭包中涵盖了someFunctionThatTakesAClosure函数中的局部变量等的引用
  • @H_403_29@myClosure=closure
  • @H_403_29@init(nibNamenibBundleOrNil:NSBundle?){
  • bundle:nibBundleOrNil)
  • @H_403_29@i=0
  • @H_403_29@varbtn=UIButton.buttonWithType(UIButtonType.System)as?UIButton
  • @H_403_29@btn!.frame=CGRectMake(0,50)
  • @H_403_29@btn!.setTitle("点击我",forState:UIControlState.Normal)
  • @H_403_29@btn!.addTarget("action",0); background-color:inherit">forControlEvents:UIControlEvents.TouchUpInside)
  • self.view.addSubview(btn)
  • @H_403_29@funcaction(){
  • @H_403_29@i=i!+1
  • //判空
  • ifmyClosure{
  • //闭包隐式调用someFunctionThatTakesAClosure函数:回调。
  • @H_403_29@myClosure!(string:"好好哦\(i)")
  • @H_403_29@}
  • 原文链接:https://www.f2er.com/swift/323383.html

    猜你在找的Swift相关文章