定义一个protocol
protocol myDelegate {
func protocol01(str:String) ->Void
}
var testdelegate:myDelegate?
self.testdelegate?.protocol01(str: "这个是协议方法")
协议回调处理
pvc.testdelegate = self;
func protocol01(str: String) {
print("协议:",str)
}
通知
添加通知
NotificationCenter.default.addObserver(self, selector:#selector(test1(notification:)), name: NSNotification.Name("testnotification"), object: nil)
//接收通知
@objc func test1(notification:NSNotification) -> Void {
let userinfo = notification.userInfo as![String:AnyObject]
print("这是个通知:",userinfo["通知"] as!String)
}
//释放移除通知
deinit {
NotificationCenter.default.removeObserver(self)
}
发送通知
NotificationCenter.default.post(name: NSNotification.Name("testnotification"), object: self, userInfo:["通知":"通知传值"])
网友评论