swift 委托代理传值

mac2022-06-30  68

委托代理

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) //值已经传过来了 } }

 

转载于:https://www.cnblogs.com/ningmengcao-ios/p/5952872.html

最新回复(0)