Swift中闭包实现OC的block传值

基本操作就是在第二个页面定义一个闭包函数,然后在第一个页面将定义好的函数,通过函数指针传递到第二个页面,然后就阔以了。废话不多说,直接上代码

//
//  ViewController.swift
//  SwiftClosure
//
//  Created by 程磊 on 16/4/15.
//  copyright © 2016年 AA租车. All rights reserved.
//

import UIKit


class ViewController: UIViewController {
    var label :UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,typically from a nib.
        label = UILabel.init(frame: CGRectMake(30,80,200,60));
        label.text = "我是第一页的文字";
        label.numberOfLines = 0;
        label.textColor = UIColor.blackColor();
        self.view.addSubview(label);
        let btn = UIButton.init(type: UIButtonType.System);
        btn.frame = CGRectMake(60,150,50);
        btn.setTitle("点我跳入下一页",forState: UIControlState.normal);
        btn.addTarget(self,action: #selector(btnClick),forControlEvents: UIControlEvents.TouchUpInside);
        self.view .addSubview(btn);
    }
    //要进行传递的函数,注意参数类型及个数是否与下个页面定义的闭包函数格式相同
    func changeLabelTextClosure(string: String) -> Void {
        label.text = string;
    }
    func btnClick() -> Void {
        let secondVC = SecondViewController();
        //将当前changeLabelTextClosure函数指针传到第二个界面,第二个界面的闭包拿到该函数指针后会进行回调该函数
        secondVC.secondViewControllerClosure = changeLabelTextClosure;
        self.presentViewController(secondVC,animated: true) { 
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // dispose of any resources that can be recreated.
    }
}

//
//  SecondViewController.swift
//  SwiftClosure
//
//  Created by aayongche on 16/4/15.
//  copyright © 2016年 AA租车. All rights reserved.
//

import UIKit

/* 定义一个类似于OC中的block快代码,其中第一个页面中定义的函数参数个数以及类型必须按照下面的格式,
 从而确保在第一个页面定义的函数指针可以正确的传递到第二个页面,
 从而使第二个页面的闭包拿到第一个页面函数指针进行回调
 */
typealias TwoViewControllerClosure = (string :String) -> Void;

class SecondViewController: UIViewController {

    var secondViewControllerClosure :TwoViewControllerClosure?
    let secondStr = "Hello World,This is Swift Closure"

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        self.view.backgroundColor = UIColor.whiteColor();

        let btn = UIButton.init(type: UIButtonType.System);
        btn.frame = CGRectMake(50,100,220,50);
        btn.backgroundColor = UIColor.redColor();
        btn.setTitle("点我传值到上个页面",forControlEvents: UIControlEvents.TouchUpInside);
        self.view.addSubview(btn);
    }

    func btnClick() -> Void {
        if (secondViewControllerClosure != nil) {
            secondViewControllerClosure!(string: secondStr);
        }
        self.dismissViewControllerAnimated(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.
    }
    */

}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...