创建两个UIViewController,A,B
//B控制器
class B: UIViewController,MyDelegate {
//实现代理
let a = A()
a.delegate = self
//执行代理方法
func testDelegate(_ str: String, str2: String) {
let a = str
let b = str2
print(a,b)
}
}
//A控制器
import UIKit
protocol MyDelegate {
func testDelegate(_ str:String,str2:String) -> Void
}
class A: UIViewController {
//创建对象
var delegate:MyDelegate?
}
//执行代理,一般放在响应事件中,如button的点击事件
self.delegate?.testDelegate(“aa”, str2: “bb”)
如上所述,B通过实现A代理跳转到A视图,当从A返回B时执行代理方法,从而完成A到B的代理传值