1、通知:
//创建一个通知中心
let notifacationName:String = "NameCustom";
NotificationCenter.default.addObserver(self, selector: #selector(receiveValue), name:NSNotification.Name(rawValue: notifacationName) , object: nil);
实现通知的方法:
@objc func receiveValue(notification:NSNotification)->Void{
var dicTmp:Dictionary = notification.userInfo!;
self.displayText1.text = dicTmp["key2"] as? String;
}
通知响应:
let notifacationName:String = "NameCustom";
let dic:Dictionary = ["key2":"张三李四全集"]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: notifacationName), object: nil, userInfo: dic)
2、协议:
//协议的创建
protocol TranslateValueDelegate {
func translateValue(num:Int)
}
//创建协议的实例
var delegate: TranslateValueDelegate?
调用:
self.delegate?.translateValue(num: 3)
协议的继承:
class ViewController: UIViewController,TranslateValueDelegate //多个协议使用‘,’隔开
let tmpSecondVC:SecondViewController = SecondViewController();
tmpSecondVC.delegate = self;
协议方法的实现:
func translateValue(num: Int) {
let tmp:String = String(num);
self.displayText.text = tmp;
}