zoukankan      html  css  js  c++  java
  • 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
        
        }

  • 相关阅读:
    数据库信息 (表名 行数 堆 集群 非聚集)的查询
    jquerygalleryview2.0 漂亮多样化的图片特效(多项自定义)
    枚举 EnumDescription 位运算 权限 sql c#
    vs2010缓存类
    透明遮罩层
    app_offline.htm的作用
    XmlToJSON(c#)
    jquery性能最佳实践
    .net面试问答(大汇总)
    apk反编译
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4889039.html
Copyright © 2011-2022 走看看