u16suzuの blog

日々学んだことのメモブログです。

NotificationCenter

// pchファイルで通知を定義
#define notification @"notification"

// Notification 送信
NSNotification *notification = [NSNotification notificationWithName:HideResultViewNotification object:self];
[[NSNotificationCenter defaultCenter] postNotification:notification];

// Notification 受信登録
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(doSomething) name:notification object:nil];


// Notification 受信時に実行するメソッド
-(void)doSomething{
}

UserInfoとして Object を受信側に渡したい場合

// 送信
 [[NSNotificationCenter defaultCenter] postNotificationName:notificationTwitterIconURLUpdated object:self userInfo:@{@"user_id": @"abcd"}];

// 受信
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(updateUserIcon:) name:notificationTwitterIconURLUpdated object:nil];

// 実行メソッド
- (void)updateUserIcon:(NSNotification *)aNotification{
    NSString *user_id = [aNotification userInfo][@"user"];
}