2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift4 Notification NotificationCenter の使い方

Last updated at Posted at 2019-03-26
//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)
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?