2009年09月21日

通知センタ

通知センタ(NSNotification)の使用方法。

■通知の受信側オブジェクトには通知センタへの登録と、通知の受信の2つが必要。

recieve.h
@class NSNotification;

- (void) setDone:(NSNotification *)aNotification;
- (void) setCancel:(NSNotification *)aNotification;

通知の受信メソッドの定義をする。

recieve.m
#import "recieve.h"

- (void)viewDidLoad
{
// 通知センタ登録
NSNotificationCenter* center;
center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(setDone:)
name:@"setDoneNotification" object:nil];
[center addObserver:self selector:@selector(setCancel:)
name:@"setCancelNotification" object:nil];
}

- (void) setDone:(NSNotification *)aNotification;
{
// 通知の送信側オブジェクト(send.m)でポストしたuserInfoは、[aNotification userInfo]で取得することができる。
NSLog(@"userInfo=%@", [aNotification userInfo]);

// Picker未選択の場合、NSDictionatyであるuserInfoの各項目には"(null)"がセットされるので、適宜処理すること。
if ([[[aNotification userInfo] objectForKey:@"hoge"] isEqualToString:@"(null)"]) {
// "(null)"の場合の処理
} else {
// "(null)"以外の場合の処理
}
}

- (void) setCancel:(NSNotification *)aNotification;
{

}



■通知の送信(ポスト)側オブジェクトでは、送信メソッドの定義をする。
send.h
@class NSNotification;

- (IBAction)doneButton:(id)sender;
- (IBAction)cancelButton:(id)sender;



send.m
#import "send.h"
- (IBAction)doneButton:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"setDoneNotification" object:self userInfo:pickerData];

}

- (IBAction)cancelButton:(id)sender
{

[[NSNotificationCenter defaultCenter] postNotificationName:@"setCancelNotification" object:self];

}

 
オブザーバ、オブジェクト、セレクタ、通知名の名称は、送信側、受信側で合わせること。 

ラベル:iPhone
posted by mobileDeveloper at 22:03 | Comment(0) | TrackBack(0) | 通知センタ | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。
※ブログオーナーが承認したコメントのみ表示されます。

この記事へのトラックバック