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

简介:

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

一、第一个界面

//  Created by 秦志伟 on 14-6-13.
import UIKit

class 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中的typedef
typealias sendValueClosure=(string:String)->Void
class 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. } */ }


转载请注明!!


本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5345750.html,如需转载请自行联系原作者 





相关文章
|
3月前
|
存储 Swift
Swift 语言:什么是闭包(Closure)?它们与函数的区别是什么?
Swift 语言:什么是闭包(Closure)?它们与函数的区别是什么?
35 1
|
4月前
|
存储 Swift
Swift中,函数和闭包
Swift中,函数和闭包
34 1
|
6月前
|
Swift
swift 闭包简写实际参数名$0、$1等理解
swift 闭包简写实际参数名$0、$1等理解
29 0
|
Swift
Swift和OC控制器互相跳转
Swift和OC控制器互相跳转
287 0
|
Swift
Swift - 用装有控制器name的数组for循环批量创建控制器(string转UIViewController)
Swift - 用装有控制器name的数组for循环批量创建控制器(string转UIViewController)
74 0
|
存储 编译器 开发者
Swift-进阶 09:闭包(二)逃逸闭包 & 非逃逸闭包
Swift-进阶 09:闭包(二)逃逸闭包 & 非逃逸闭包
448 0
Swift-进阶 09:闭包(二)逃逸闭包 & 非逃逸闭包
|
存储 Swift C++
Swift-进阶 09:闭包(一)使用&捕获原理
Swift-进阶 09:闭包(一)使用&捕获原理
430 0
Swift-进阶 09:闭包(一)使用&捕获原理
|
Swift
swift微博第15天(新版的判断以及跟控制器的切换)
swift微博第15天(新版的判断以及跟控制器的切换)
144 0
swift微博第15天(新版的判断以及跟控制器的切换)
|
JSON Swift 数据格式
swift微博第3天(动态加载控制器)
swift微博第3天(动态加载控制器)
99 0
swift微博第3天(动态加载控制器)
|
Swift
swift微博第2天(命名空间和控制器字符串)
swift微博第2天(命名空间和控制器字符串)
114 0
swift微博第2天(命名空间和控制器字符串)

相关课程

更多