微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/swift/324062.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐