NSNotificationCenter
An NSNotificationCenter object (or simply, notification center) provides a mechanism for broadcasting information within a program. An NSNotificationCenter object is essentially a notification dispatch table.
NSNotificationCenter 객체는 프로그램내 broadcasting에 대한 메커니즘에을 제공한다.
이 객체는 통지처리 테이블(notification dispatch table)을 기본으로 한다
Objects register with a notification center to receive notifications (NSNotification objects) using the addObserver:selector:name:object: or addObserverForName:object:queue:usingBlock: methods. Each invocation of this method specifies a set of notifications. Therefore, objects may register as observers of different notification sets by calling these methods several times.
객체는 addObserver:selector:name:object: 나 addObserverForName:object:queue:usingBlock: methods를
사용하여 NSNotificationCenter 객체를 알림센터(notification center)에 등록한다
When an object (known as the notification sender) posts a notification, it sends an NSNotification object to the notification center. The notification center then notifies any observers for which the notification meets the criteria specified on registration(지정된 표준 등록) by sending them the specified notification message, passing the notification as the sole argument.
객체(notification sender로 알려진..) 알림을 발송 할 때 알림센터에 NSNotification 객체를 보낸다.
A notification center maintains a notification dispatch table which specifies a notification set for a particular observer. A notification set is a subset of the notifications posted to the notification center. Each table entry contains three items:
알림센터는 특별한 observer에 대한 알림집합이 명시된 알림 발송 테이블을 유지한다. 알림집합은 알림센터에 보내지는 알림의 하위집합이다. 각 테이블 엔트리는 3가지 항목을 포함한다
Notification observer: Required. The object to be notified when qualifying notifications are posted to the notification center.
Notification name: Optional. Specifying a name reduces the set of notifications the entry specifies to those that have this name.
Notification sender: Optional. Specifying a sender reduces the set of notifications the entry specifies to those sent by this object.
Table 1 shows the four types of dispatch table entries and the notification sets they specify. (This table omits the always present notification observer.)
defaultCenter
Returns the process’s default notification center.
+ (id)defaultCenter
Return Value
The current process’s default notification center, which is used for system notifications.
Availability
- Available in iOS 2.0 and later.
예제
옵저버 등록
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self // 센터에 옵저버 등록
selector:@selector(reachabilityChanged:) // 옵저버가 가지고 있는 메소드 셀렉터 -> self(appDelegate)의 reachabilityChanged: 호출
name:Na_STR_REACHABILITY_NOTIFICATION // 메세지를 보내서 알리고 싶은 노티피케이션명을 지정
object:nil // 특정 객체가 포스팅한 통지만 받고 싶을 때 지정
];
옵저버 삭제
[removeObserver:@selector(reachabliityChanged)]
----NotificationObj.h--------
#import <Foundation/Foundation.h>
@interface NotificationObj : NSObject {
}
- (void) callChange;
- (void) regNotification;
- (void) postNotification;
@end
----NotificationObj.m--------
#import <Foundation/Foundation.h>
#import "NotificationObj.h"
@implementation NotificationObj
- (void) regNotification {
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
// 네트워크 연결 상태가 변경할 때 마다 노티피케이션을 포스트하기 위해 defaultCenter에 옵저버 등록
[defaultCenter addObserver:self
selector:@selector(callChange)
name:@"CALL_CHANGE"
object:nil];
}
- (void) postNotification {
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter postNotificationName:@"CALL_CHANGE"
object:self];
}
- (void) callChange {
NSLog(@"Notification Post");
}
@end
----main.m--------
#import <Foundation/Foundation.h>
#import "NotificationObj.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NotificationObj * nObj = [[NotificationObj alloc] init];
[nObj regNotification];
[nObj postNotification];
[pool release];
return 0;
}