swift传值:利用代理(delegate)和闭包(closure)

本文介绍在Swift中如何通过Delegate和Closure实现值的传递,包括两种方式的具体实现过程及代码示例。

swift使用delegate和closure进行传值:类似oc的代理和block


firstViewController.swift文件

//

//  firstViewController.swift

//  testPageControl

//

//  Created by yinlinlin on 15/12/29.

//  Copyright © 2015 yinlinlin. All rights reserved.

//


import UIKit


class firstViewController:UIViewController,passValueDelegate {


    var passValueLabel:UILabel!

    override func viewDidLoad() {

        super.viewDidLoad()


        self.view.backgroundColor =UIColor.whiteColor()

        let pushBtn =UIButton(type: .Custom)

        pushBtn.frame =CGRectMake(100,150, kMainScreenWidth -200, 40)

        pushBtn.setTitle("secondCon", forState: .Normal)

        self.view.addSubview(pushBtn)

        pushBtn.addTarget(self, action:Selector("pushTosecondViewController"), forControlEvents: .TouchUpInside)

        pushBtn.setTitleColor(UIColor.redColor(), forState: .Normal)

        passValueLabel =UILabel.init(frame:CGRectMake(20,100, kMainScreenWidth -40, 30))

        passValueLabel.backgroundColor =UIColor.grayColor()

        self.view.addSubview(passValueLabel)

        

        // Do any additional setup after loading the view.

    }


    func pushTosecondViewController() {

        let secondCon =secondViewController()

        

        //利用闭包传值:跟ocblock很像

        secondCon.myClosure = {

            [unowned self](newValue:AnyObject) ->Void

            in

            self.passValueLabel.text = newValueas? String

        }

        secondCon.delegate =self

        

        self.navigationController?.pushViewController(secondCon, animated:true)

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

//MARK: - passValueDelegate代理回调

    func passValue(newValue:AnyObject) {

        print("接收代理回调的值:\(newValue)")

        passValueLabel.text = newValueas? String

    }

    /*

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

    }

    */


}



secondViewController.swift文件

//

//  secondViewController.swift

//  testPageControl

//

//  Created by yinlinlin on 15/12/29.

//  Copyright © 2015 yinlinlin. All rights reserved.

//


import UIKit


//利用代理传值

protocol passValueDelegate:NSObjectProtocol {

    

    func passValue(newValue:AnyObject)

}


//闭包回调传值

typealias sendValueClosure = (newValue:AnyObject) ->Void


class secondViewController: UIViewController {


    var myClosure:sendValueClosure?

    weakvar delegate:passValueDelegate?

    var textField:UITextField!

    override func viewDidLoad() {

        super.viewDidLoad()


        self.view.backgroundColor =UIColor.whiteColor()


        textField = UITextField.init(frame: CGRectMake(20, 100, kMainScreenWidth - 40, 30))

        textField.backgroundColor =UIColor.yellowColor()


        textField.placeholder ="输入newValue"

        self.view.addSubview(textField)

        

        let delegateBtn = UIButton(type: .Custom)

        delegateBtn.frame = CGRectMake(20, 150, kMainScreenWidth/2.0 -40, 40)

        delegateBtn.setTitleColor(UIColor.redColor(), forState: .Normal)

        delegateBtn.setTitle("delegate传值", forState: .Normal)

        self.view.addSubview(delegateBtn)

        delegateBtn.addTarget(self, action:Selector("delegatePassValueBtnPressed"), forControlEvents: .TouchUpInside)

        

        let closureBtn = UIButton(type: .Custom)

        closureBtn.frame = CGRectMake(kMainScreenWidth/2.0+20,150, kMainScreenWidth/2.0 -40, 40)

        closureBtn.setTitle("闭包传值", forState: .Normal)

        closureBtn.setTitleColor(UIColor.redColor(), forState: .Normal)

        self.view.addSubview(closureBtn)

        closureBtn.addTarget(self, action:Selector("closurePassValueBtnPressed"), forControlEvents: .TouchUpInside)

        // Do any additional setup after loading the view.

    }


    //MARK: - 代理传值

    func delegatePassValueBtnPressed(){

        delegate?.passValue(textField.text!)

        self.navigationController?.popViewControllerAnimated(true)

    }

    //MARK: - 闭包传值

    func closurePassValueBtnPressed(){

        if myClosure !=nil {

            myClosure!(newValue: textField.text!)

        }

        self.navigationController?.popViewControllerAnimated(true)

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    


    /*

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

    }

    */


}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值