Swift中闭包实现OC的block传值

前端之家收集整理的这篇文章主要介绍了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.
    }
    */

}
原文链接:https://www.f2er.com/swift/324062.html

猜你在找的Swift相关文章