swift中的閉包傳值(block)

原文地址:http://www.myexception.cn/operating-system/1684429.html
http://www.cocoachina.com/bbs/read.php?tid=230724

原文如下:

 

Swift利用閉包(closure)來實現傳值-->前後兩個控制器的反向傳值

利用了大約一個多小時來搞明白OCBlocks反向傳值和SwiftClosure反向傳值的差別,下面直接貼上代碼:

一、第一個界面

//  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.

    }

    

    

   

    

}


二、第二個界面

//  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.

    }

    

    

   

    

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章