1,定义 protocol
protocol callBackDelegate {
func callbackDelegatefuc(backMsg:String)
}
2,在需要回调类定义协议成员变量 并在需要回调的方法内编写具体回调的逻辑
class ProcessData: NSObject{
var delegate:callBackDelegate?
func processMethod(cmdStr:String?){
if((delegate) != nil){
delegate?.callbackDelegatefuc(backMsg: "backMsg---by delegate")
}
}
}
3 在需要监听回调的类上实现协并且实现协议的方法 并且设置设置协议监听
class ViewController: UIViewController,callBackDelegate{
override func viewDidLoad() {
super.viewDidLoad()
let process=ProcessData()
process.delegate=self
//执行函数
process.processMethod(cmdStr: "aaa")
}
//delegate回调
func callbackDelegatefuc(backMsg:String){
print(backMsg)
}
}