좋아, 나는 CFNotificationCenter의 작은 예제를 썼다. 일반적으로 대규모 프로젝트에는 CoreFoundation을 사용하지 않고 대신 Foundation을 사용합니다. Objective-C에서이 프로젝트를 실제로 작성하는 경우 (내가 태그에서 추측 한대로) NSNotificationCenter을 사용하는 것이 좋습니다.
#include <CoreFoundation/CoreFoundation.h>
void notificationCallback (CFNotificationCenterRef center,
void * observer,
CFStringRef name,
const void * object,
CFDictionaryRef userInfo) {
CFShow(CFSTR("Received notification (dictionary):"));
// print out user info
const void * keys;
const void * values;
CFDictionaryGetKeysAndValues(userInfo, &keys, &values);
for (int i = 0; i < CFDictionaryGetCount(userInfo); i++) {
const char * keyStr = CFStringGetCStringPtr((CFStringRef)&keys[i], CFStringGetSystemEncoding());
const char * valStr = CFStringGetCStringPtr((CFStringRef)&values[i], CFStringGetSystemEncoding());
printf("\t\t \"%s\" = \"%s\"\n", keyStr, valStr);
}
}
int main (int argc, const char * argv[]) {
CFNotificationCenterRef center = CFNotificationCenterGetLocalCenter();
// add an observer
CFNotificationCenterAddObserver(center, NULL, notificationCallback,
CFSTR("MyNotification"), NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
// post a notification
CFDictionaryKeyCallBacks keyCallbacks = {0, NULL, NULL, CFCopyDescription, CFEqual, NULL};
CFDictionaryValueCallBacks valueCallbacks = {0, NULL, NULL, CFCopyDescription, CFEqual};
CFMutableDictionaryRef dictionary = CFDictionaryCreateMutable(kCFAllocatorDefault, 1,
&keyCallbacks, &valueCallbacks);
CFDictionaryAddValue(dictionary, CFSTR("TestKey"), CFSTR("TestValue"));
CFNotificationCenterPostNotification(center, CFSTR("MyNotification"), NULL, dictionary, TRUE);
CFRelease(dictionary);
// remove oberver
CFNotificationCenterRemoveObserver(center, NULL, CFSTR("TestValue"), NULL);
return 0;
}
이 예는 관찰자, 그것에 게시물 간단한 사전을 생성하고, 관찰자를 제거합니다 : 더 adue없이, 여기에 예입니다. CFNotificationCenter에 대한 자세한 내용은 Apple's CFNotificationCenter Reference에서 확인할 수 있습니다.
CFNotificationCenter 또는 NSNotificationCenter 사용에 대한 예가 필요합니까? CFNotificationCenter는 NSNotificationCenter와 동일한 CoreFoundation입니다. 그들은 똑같은 일을합니다. Stackoverflow에 NSNotificationCenter의 [예제] (http://stackoverflow.com/questions/2191594/how-to-send-and-receive-message-through-nsnotificationcenter-in-objective-c)가 도움이 될 수 있습니다. 아웃. –