• swift中闭包的用法


    正所谓一法通,万法皆通,具体什么意思,你们自己领悟吧。

    闭包我想不用多介绍了吧,学过OC的都应该知道,其功能和OC里的Block一样,和通知,在功能上也有相同之处。

    先看个界面效果图,用通知也能做出来。我在上面一篇将通知的,也是写的这个功能。

    //
    //  ViewController.swift
    //  Swift闭包的用法
    //
    //  Created by mac on 16/2/5.
    //  Copyright © 2016年 ZY. All rights reserved.
    //
    
    import UIKit
    
    class ViewController: UIViewController {
    
        
        @IBOutlet weak var textF: UITextField!
        
        @IBOutlet weak var pushButtonAction: UIButton!
    
        
        override func viewDidLoad() {
            super.viewDidLoad()
           
            pushButtonAction.addTarget(self, action:"pushButtonAction1:", forControlEvents: UIControlEvents.TouchUpInside);
            
            
        }
    
        
        func pushButtonAction1(btn:UIButton){
            
            
            let viewC = SeconViewController(nibName:"SeconViewController",bundle: nil) as SeconViewController;
           
    //        参数回调
            viewC.textback { (text) -> Void in
        
                self.textF.text = text as String;
        
            };
            
            self.presentViewController(viewC, animated: true, completion: { () -> Void in
                
                
            });
    //        let a = 25;
    //        print("(a)");
            
        }
        
    
    }
    
    //
    //  SeconViewController.swift
    //  Swift闭包的用法
    //
    //  Created by mac on 16/2/5.
    //  Copyright © 2016年 ZY. All rights reserved.
    //
    
    import UIKit
    
    class SeconViewController: UIViewController {
    
        @IBOutlet weak var textF: UITextField!
        
        
        @IBOutlet weak var button: UIButton!
        
        typealias newFuncy = (text : NSString) -> Void;
       
        var _myFuncy = newFuncy?();
        
        
        
        override func viewDidLoad() {
            super.viewDidLoad()
    
            button.addTarget(self, action:"pushButtonAction1:", forControlEvents: UIControlEvents.TouchUpInside);
            
            
        }
    //    闭包值的传递方法
        func textback(mathFunction:(text : NSString) -> Void){
            
            _myFuncy = mathFunction;
            
            
        }
    
    
        
        func pushButtonAction1(btn:UIButton){
            
            if((_myFuncy) != nil){
    //            调用闭包
               _myFuncy!(text: textF.text!);
                
            }
            
            
            self.dismissViewControllerAnimated(true) { () -> Void in
    
    
            };
            
            
        }
    
       //扩展方面
    // 闭包的另类用法,用于接口调用 func dataBack(str:String , mathFunction:(text:String) -> Void){ print("(str)"); let newText = "erwdd"; mathFunction(text: newText); } // 类方法 class func ushButtonAc(){ } }

  • 相关阅读:
    temp12
    temp111
    test.c
    vipLogin.c
    services.c
    request.c
    managerLogin.c
    将博客搬至CSDN
    SpringMabatis整合项目mybatis-configuration.xml核心配置
    logback-test.xml配置文件模板
  • 原文地址:https://www.cnblogs.com/zxh-iOS/p/5185565.html
Copyright © 2020-2023  润新知