• 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(){ } }

  • 相关阅读:
    更新部分字段 NHibernate
    无法显示 XML 页。 使用 XSL 样式表无法查看 XML 输入。请更正错误然后单击 刷新按钮,或以后重试的解决办法
    初识使用Apache MINA 开发高性能网络应用程序
    生产者消费者问题理解与Java实现
    国内HTML5前段开发框架汇总
    mongodb的sharding架构搭建
    spring配置声明式事务
    如何设计页面固定广告的效果
    结合实际问题浅谈如何使用蒙特卡罗算法模拟投资分析
    多线程实现资源共享的问题学习与总结
  • 原文地址:https://www.cnblogs.com/zxh-iOS/p/5185565.html
Copyright © 2020-2023  润新知