• swift-delegate(代理)或者block传值


    1:delegate或者block传值

    import UIKit
    class ViewController: UIViewController,TestDelegatePassValueDelegate {
    
        @IBOutlet weak var TFContent: UITextField!
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        @IBAction func delegateOrBlockBtnClick(sender: AnyObject) {
            TFContent.text = ""
            let storyBoard = UIStoryboard(name: "Main", bundle: nil)
            let testVCtl = storyBoard.instantiateViewControllerWithIdentifier("TestDelegateOrBlockViewController") as!TestDelegateOrBlockViewController;
            /**设置代理*/
            testVCtl.delegate = self
            /**block回调传值*/
            testVCtl.passValueUseBlock { (valueString) -> Void in
                
                self.TFContent!.text = valueString  as String
                
            }
            self.navigationController!.pushViewController(testVCtl, animated: true);
            
            
        }
        /**代理函数*/
        func passValueUseDelegate(valueString: NSString)
        {
            self.TFContent!.text = valueString as String
        }
       
    }
    import UIKit
    import Foundation
    /**定义一个block*/
    typealias TestBlockCompleteHandler = (valueString: NSString)->Void
    /**声明定义协议*/
    protocol TestDelegatePassValueDelegate:NSObjectProtocol
    {
        func passValueUseDelegate(valueString: NSString)
    
    }
    
    class TestDelegateOrBlockViewController: UIViewController
    {
    
        @IBOutlet weak var TFContent: UITextField!
        var delegate:TestDelegatePassValueDelegate?
       
        var myBlockHandler:TestBlockCompleteHandler?
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
        }
    
        override func didReceiveMemoryWarning()
        {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        
        
      //MARK: - 用代理来实现传值
        @IBAction func delegateBtnClick(sender: AnyObject)
        {
            
             TFContent.resignFirstResponder()
            if ((delegate != nil) && (delegate?.respondsToSelector("passValueUseDelegate:") != nil))
            {
              delegate?.passValueUseDelegate(TFContent.text!)
            
            }
            self.navigationController?.popViewControllerAnimated(true)
            
            
            
        }
        
        //MARK: - 用block来实现传值
        @IBAction func blockBtnClick(sender: AnyObject)
        {
            
            TFContent.resignFirstResponder()
            myBlockHandler?(valueString: self.TFContent.text!)
            self.navigationController?.popViewControllerAnimated(true)
        
        }
        
        func passValueUseBlock(blockHandler: TestBlockCompleteHandler)
        {
          myBlockHandler = blockHandler
        
        }

  • 相关阅读:
    基于yum的方式安装Cloudera Manager Agent(使用Mysql 8.0版本)
    数据库基础SQL知识面试题二
    Zabbix Server 自带模板监控更加灵活MySQL数据库
    Zabbix Server 自带模板监控有密码MySQL数据库
    Zabbix Server 自带模板监控无密码MySQL数据库
    Zabbix Server 监控Web页面
    HDFS集群常见报错汇总
    Hadoop ha CDH5.15.1-hadoop集群启动后,集群容量不正确,莫慌,这是正常的表现!
    FastDFS + Nginx代理方式访问
    FastDFS 分布式文件系统部署实战及基本使用
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4889039.html
Copyright © 2020-2023  润新知