Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Latest commit

 

History

History
96 lines (62 loc) · 4.3 KB

3-4_Notification-NotificationCenter.md

File metadata and controls

96 lines (62 loc) · 4.3 KB

参考 mixi-inc/iOSTraining 7.4 NSNotification、NSNotificationCenter を用いた通知

Notification Class Reference

NotificationCenter Class Reference

Notification Programming Topics

Cocoa Fundamentals Guide

3つ目の通知パターンは Notification、NotificationCenter を用いた通知です。

この通知パターンは NotificationCenter のシングルトンインスタンスによるブロードキャストな通知を実現することができます。

notification

Cocoa Fundamentals Guide より引用

通知の実装概要

通知を実現するためには以下のステップが必要です

  1. 通知を受けたいインスタンスに対して NotificationCenter にオブザーバ登録
  2. 通知ハンドラーの実装
  3. NotificationCenter に通知を post
  4. オブザーバ登録の解除

1. NotificationCenter にオブザーバ登録

NotificationCenter.default.addObserver(self, //[1]オブザーバとして登録
                              selector: #selector(type(of: self).recieveNotification(_:)), //[2]通知ハンドラーの指定
                                  name: NSNotification.Name(rawValue: "pushNotificationTapped"), //[3] 通知名
                                object: nil) // [4] sender の指定

[1]オブザーバとして登録

オブザーバオブジェクトを指定します。指定したオブジェクトに変化通知が届きます。

[2]通知ハンドラーの指定

通知を受け取った際に指定したセレクタが呼び出されます。

[3] 通知名

指定の通知名の通知を受け取ることが可能。nil を設定するとすべてのオブジェクトから通知を受け取る。

[4] sender の指定

指定のオブジェクトからのみ通知を受けたいときに指定。nil を設定するとすべてのオブジェクトから通知を受け取る。

2. 通知ハンドラーの実装

通知ハンドラは Notification オブジェクトを受け取るようにします。

func recieveNotification(_ notification: Notification) {
    // do something
}

通知登録とハンドラの実装を closure を使ったパターンでまとめることもできます。

NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "pushNotificationTapped"),
                                        object: nil, queue: OperationQueue.main) { notification in
    // do something
}

3. NotificationCenter に通知を post

通知を post する際に userInfo として任意のデータ(Dictionary)を指定することができます。通知の受取手は Notification の userInfo プロパティでこのデータにアクセスすることが可能です。

let dict = ["key" : "value"]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "pushNotificationTapped"), object: self, userInfo: dict)

4. オブザーバ登録の解除

NotificationCenter.default.removeObserver(self)

問題

tabBarController プロジェクトを作成し、下記の仕様を満たすプログラムを作成してください。

  • firstViewController で ボタンを押すと任意の文字列を持った userInfo を用いて通知を post する
  • secondViewController で通知を取得し、上記の任意の文字列をラベルにセットする

practice

この解答はsamples/day3/sample3-4にあります。