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

7.4 NSNotification、NSNotificationCenter を用いた通知

Jun Morimoto edited this page May 14, 2013 · 6 revisions

NSNotification Class Reference

NSNotificationCenter Class Reference

Notification Programming Topics

Cocoa Fundamentals Guide

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

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

notification

Cocoa Fundamentals Guide より引用

通知の実装概要

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

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

1. NSNotificationCenter にオブザーバ登録

[[NSNotificationCenter defaultCenter] addObserver:self //[1]オブザーバとして登録
                                         selector:@selector(recieveNotification:) //[2]通知ハンドラーの指定
                                             name:@"notificationName" //[3] 通知名
                                           object:nil]; // [4] sender の指定

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

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

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

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

[3] 通知名

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

[4] sender の指定

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

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

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

-(void)recieveNotification:(NSNotification *)notification
{
    // do something
}

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

[[NSNotificationCenter defaultCenter] addObserverForName:@"notificationName"
                                                  object:nil
                                                   queue:[NSOperationQueue mainQueue]
                                              usingBlock:^(NSNotification *note) {
                                                  // do something
                                              }];

3. NSNotificationCenter に通知を post

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

NSDictionary *dict = @{@"key":@"value"};
[[NSNotificationCenter defaultCenter] postNotificationName:@"notificationName" object:self userInfo:dict];

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

[[NSNotificationCenter defaultCenter] removeObserver:self];

問題

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

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

practice

はじめに

  1. iOSについて

  2. Xcode最初のステッフ

  3. 導入

  4. Objective C の基礎

  5. メモリ管理

  6. 1.3 UIViewController1 UIViewController のカスタマイズ(xib, autoresizing)

  7. 1.3 UIViewController1 UIViewController のカスタマイズ(storyboard)

  8. UIViewController2 - ModalViewController

  9. UIViewController2 - ModalViewController(storyboard)

  10. UIViewController3 - ライフサイクル

  11. HomeWork 1 Objective C の基本文法

  12. HomeWork 2 UIViewControllerとModalViewController

  13. HomeWork 3 UIViewController + Animation

  14. UIKit 1 - container, rotate-

  15. UINavigationController

  16. UITabController

  17. Custom Container View Controller

  18. Supporting Multiple Interface Orientations

  19. HomeWork 1 - タブバーからモーダルビューを表示する

  20. HomeWork 2 - NavigationController

  21. HomeWork 2.3 デバイスことに回転対応

  22. UIKit 2- UIView -

  23. UIView

  24. UIView のカスタマイズ

  25. UIView Animation

  26. HomeWork 1 - UIScrollView

  27. UIKit 3 - table view -

  28. UITableView について

  29. UITableViewとNavigationController

  30. custom UITableViewCell の作成

  31. UITableViewのその他のオプション、カスタマイズ

  32. HomeWork 1 - Dynamic height with a custom uitableviewcell

  33. UIKit 4 - image and text -

  34. UIImagePickerController

  35. Assets Library

  36. UITextFiled, UITextView

  37. KeyboardNotification

  38. Homework 1 - フォトの複数枚選択

  39. ネットワーク処理

  40. NSURLConnection

  41. JSONのシリアライズとデシリアライズ

  42. UIWebView

  43. ローカルキャッシュと通知

  44. NSUserDefaults, Settings Bundle

  45. NSFileManager

  46. Key Value Observing

  47. NSNotification、NSNotificationCenter を用いた通知

  48. UILocalNotification

  49. Blocks, GCD

  50. Blocks

  51. GCD

  52. 【演習】GCD,-Blocksを用いたHTTPリクエストマネージャの作成

  53. 設計とデザインパターン

  54. クラス設計 1

  55. クラス設計 2

  56. [クラス設計演習] (https://github.com/mixi-inc/iOSTraining/wiki/9.3-%E3%82%AF%E3%83%A9%E3%82%B9%E8%A8%AD%E8%A8%88%E6%BC%94%E7%BF%92)

  57. 開発ツール

  58. Instruments, デバッガ

  59. CocoaPods

  60. テスト

  61. iOS開発におけるテスト

  62. GHUnit

  63. Kiwi

  64. KIF

  65. In-App Purchase

  66. In-App Purchase

  67. 付録

  68. Tips of Xcode

  69. Auto Layout 入門

  70. Auto Layout ドリル

Edit sidebar

Clone this wiki locally