SWIFT语言实现代理传值/闭包传值

1.需求:利用代理实现反向传值(例子采用点击第二个视图控制器中的按钮来改变第一个视图控制器中的Label的内容)

一、第一个界面

 1 class ViewController: UIViewController, ChangeTestLabelDelegate {
 2     var testLabel: UILabel?
 3     override func viewDidLoad() {
 4         super.viewDidLoad()
 5         // Do any additional setup after loading the view, typically from a nib.
 6         self.title = "swift代理传值";
 7         let rightButtonItem: UIBarButtonItem = UIBarButtonItem(title: "下一页", style: .Plain, target: self, action: "nextPage");
 8         self.navigationItem.rightBarButtonItem = rightButtonItem;
 9         
10         testLabel = UILabel(frame: CGRectMake(0, 200, 320, 50));
11         testLabel?.layer.borderColor = UIColor.redColor().CGColor;
12         testLabel?.layer.borderWidth = 1;
13         testLabel?.layer.cornerRadius = 5;
14         testLabel?.text = "学雨燕";
15         testLabel?.textAlignment = NSTextAlignment.Center;
16         self.view.addSubview(testLabel!);
17     }
18 
19     func nextPage() {
20         let secondVC: SecondViewController = SecondViewController();
21         secondVC.delegate = self;
22         self.navigationController?.pushViewController(secondVC, animated: true);
23     }
24     
25     func changeLabelText(controller: SecondViewController, string: String) {
26         testLabel?.text = string;
27         println("testLabel.text == \(string)");
28     }
29     override func didReceiveMemoryWarning() {
30         super.didReceiveMemoryWarning()
31         // Dispose of any resources that can be recreated.
32     }
33 
34 
35 }

二、第二个界面

 1 import Foundation
 2 import UIKit
 3 
 4 //定义协议改变Label内容
 5 protocol ChangeTestLabelDelegate: NSObjectProtocol {
 6     //回调方法
 7     func changeLabelText(controller: SecondViewController, string: String);
 8 }
 9 
10 class SecondViewController: UIViewController {
11     var temp = 0;
12     var delegate: ChangeTestLabelDelegate?
13     override func viewDidLoad(){
14         super.viewDidLoad()
15         self.title = "SecondViewController";
16         self.view.backgroundColor = UIColor.yellowColor();
17         let rect = CGRect(x:50,y:200,150,height:50);
18         var myButton = UIButton(frame:rect);
19         myButton.center = CGPointMake(160,200);
20         myButton.setTitle("改变Label内容",forState:.Normal);
21         myButton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal);
22         myButton.addTarget(self,action:"btnClicked",forControlEvents:.TouchUpInside);
23         self.view.addSubview(myButton);
24         
25     }
26     func btnClicked(){
27         temp++;
28         println("我被点击了\(temp)次.")
29         if(delegate != nil) {
30             delegate?.changeLabelText(self, string: "学雨燕" + "\(temp)");
31         }    
32     }
33 }

 2.需求:利用闭包实现反向传值

 1 import UIKit
 2 
 3 class ViewController: UIViewController {
 4     var testLabel: UILabel?
 5     override func viewDidLoad() {
 6         super.viewDidLoad()
 7         // Do any additional setup after loading the view, typically from a nib.
 8         self.title = "swift闭包传值";
 9         let rightButtonItem: UIBarButtonItem = UIBarButtonItem(title: "下一页", style: .Plain, target: self, action: "nextPage");
10         self.navigationItem.rightBarButtonItem = rightButtonItem;
11         
12         testLabel = UILabel(frame: CGRectMake(0, 200, 320, 50));
13         testLabel?.layer.borderColor = UIColor.redColor().CGColor;
14         testLabel?.layer.borderWidth = 1;
15         testLabel?.layer.cornerRadius = 5;
16         testLabel?.text = "swift闭包";
17         testLabel?.textAlignment = NSTextAlignment.Center;
18         self.view.addSubview(testLabel!);
19     }
20 
21     func nextPage() {
22         let secondVC: SecondViewController = SecondViewController();
23         secondVC.initWithClosure(someFunctionThatTakesAClosure);
24         self.navigationController?.pushViewController(secondVC, animated: true);
25     }
26     func someFunctionThatTakesAClosure(string:String) -> Void {
27         testLabel!.text = string
28     }
29     override func didReceiveMemoryWarning() {
30         super.didReceiveMemoryWarning()
31         // Dispose of any resources that can be recreated.
32     }
33 
34 
35 }
 1 import Foundation
 2 import UIKit
 3 //类似于OC中的typedef
 4 typealias changeLabelTextClosure = (string:String) -> Void;
 5 
 6 class SecondViewController: UIViewController {
 7     var myClosure: changeLabelTextClosure?   //声明一个闭包
 8     var temp = 0;
 9     func initWithClosure(closure: changeLabelTextClosure?) {
10         myClosure = closure; //将函数指针赋值给myClosure闭包,该闭包中涵盖了someFunctionThatTakesAClosure函数中的局部变量等的引用
11     }
12     override func viewDidLoad(){
13         super.viewDidLoad()
14         self.title = "SecondViewController";
15         self.view.backgroundColor = UIColor.yellowColor();
16         let rect = CGRect(x:50,y:200,150,height:50);
17         var myButton = UIButton(frame:rect);
18         myButton.center = CGPointMake(160,200);
19         myButton.setTitle("改变Label内容",forState:.Normal);
20         myButton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal);
21         myButton.addTarget(self,action:"btnClicked",forControlEvents:.TouchUpInside);
22         self.view.addSubview(myButton);
23         
24     }
25     func btnClicked(){
26         temp++;
27         println("我被点击了\(temp)次.")
28             if (myClosure != nil){
29                 //闭包隐式调用someFunctionThatTakesAClosure函数:回调。
30                 myClosure!(string: "闭包传值\(temp)");
31             }
32     }
33 }
原文地址:https://www.cnblogs.com/ShawnLi/p/4446059.html