swift 委托代理传值

委托代理

1.定义个协议

2.声明一个委托代理

3.指定委托代理,调用委托实现的协议方法

4实现LoadingDelegate协议

代码如下:

import UIKit
//1.定义个协议
protocol loadingDeleagte {
    func didLoading(text: String)
}

class HomeViewController: UIViewController {

    @IBOutlet weak var textFlied: UITextField!
    
    //2.声明一个委托代理
    var delegate: loadingDeleagte?
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    
    @IBAction func backBtnClick(_ sender: AnyObject) {
        
        print("点击了")
        //3实例一个 ViewController类
        let loading = ViewController()
        //指定委托代理是 loading 的实例
        delegate = loading
        //调用委托实现的协议方法
        delegate?.didLoading(text: textFlied.text!)
    }
 
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        UIApplication.shared.keyWindow?.endEditing(true)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

  
}
import UIKit

class ViewController: UIViewController {

    
    @IBOutlet weak var textLable: UILabel!
    @IBOutlet weak var NextBtn: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
    
   
        
    }

}

//4实现LoadingDelegate协议
extension ViewController : loadingDeleagte {

    func didLoading(text: String) {
    
        print(text)
        
        //值已经传过来了
        
    }
}

 

posted @ 2016-10-12 14:57  _水畔竹汐  阅读(1319)  评论(0编辑  收藏  举报