//Step1
extension Notification.Name {
static let notifyName = Notification.Name("notifyName")
}
//Step2 Fire notify
NotificationCenter.default.post(name: .notifyName, object: nil)
//Step3 Add Observer
NotificationCenter.default.addObserver(self, selector: #selector(doSomething(_:)), name: .notifyName, object: nil)
//Step4 Do somthing
@objc func doSomething(_ notification: Notification)
{
print("doSomething")
}
//Step4 Remove Observer
// すべて解除
center.removeObserver(self)
// 特定の通知のみ解除
center.removeObserver(self,
name: .UIApplicationDidBecomeActive,
object: nil)
More than 5 years have passed since last update.
Swift4 Notification NotificationCenter の使い方
Last updated at Posted at 2019-03-26
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme