SWIFT語言實現代理傳值/閉包傳值

來源:互聯網
上載者:User

標籤:

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,width: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 = string28     }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,width: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 }

 

SWIFT語言實現代理傳值/閉包傳值

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.