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
        
        }

  • 相关阅读:
    【转】开发人员一定要加入收藏夹的网站
    ASP.NET页面之间传递值的几种方式
    查询数据库中字段内容相同的记录
    将csv文件导入到数据库中
    XMLHttpRequest对象(三)
    Ajax基础(一)
    Ajax浏览器支持(二)
    javascript获取浏览器的
    SQL SERVER 通过链接服务器访问ORACLE 包中的存储过程 带参数
    SQL 添加链接服务器
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4889039.html
Copyright © 2011-2022 走看看