Swift 代理
swift中代理的使用
- 首先定义一份协议。
protocol DetailsViewControllerDelegate: NSObjectProtocol{ func fetchBackString(str: String) }
- 声明代理属性
weak var delegate: DetailsViewControllerDelegate?
- 遵守代理并实现
class ViewController: DetailsViewControllerDelegate
let VC = segue.destination as! DetailsViewController VC.delegate = self
//MARK: - DetailsViewControllerDelegate(代理) func fetchBackString(str: String) { NSLog(str, "") }
- 使用
delegate!.fetchBackString(str: "代理返回数据:\(area.name)")