Swift利用閉包(closure)來實現傳值-->先後兩個控制器的反向傳值閉包
利用了大約一個多小時來搞明白OC中Blocks反向傳值和Swift中Closure反向傳值的差異,下面直接貼上代碼:app
1、第一個界面ide
// 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. } */ }
2、第二個界面函數
// 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. } */ }