2011-08-06 10 views
4

私はまだこれにはかなり新しいですが、私は例の助けを借りて、本当にすぐに学習してきました。私は現在、ある実行中のプログラムから別のプログラムへ通知をポストすることを検討しています.CFNotificationCenterは今後の進め方です。唯一の問題は、私はそれを使用するために動くことができず、リンゴのビデオビュワー以外の例はないようです。CFNotificationCenterの使用例?

誰もが私は通知を掲示する一つのアプリケーションを書くことができ、1つのテスト通知とdoSomethingの()を受信するように設定する方法についてのミニ例を供給することができるだろう;?どんな助けでも大歓迎です!

+1

あなたはCFNotificationCenter、またはNSNotificationCenterを使って上の例をし​​たいですか? CFNotificationCenterはNSNotificationCenterに相当するCoreFoundationです。彼らは同じことをする。 [例]は既に存在し(http://stackoverflow.com/questions/2191594/how-to-send-and-receive-message-through-nsnotificationcenter-in-objective-c)NSNotificationCenterのStackOverflowの上であなたを助けるかもしれませんでる。 –

答えて

9

さてさて、私は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をご覧ください。

+0

ありがとう、これらのビットとピースは私が探していたもので、今はどのように動作するのか理解しています。私は2つのアプリケーションの間でこれをやっているので、ローカルアプリケーションの代わりにCFNotificationCenterGetDistributedCenterを使用しました。しかし、そうでなければ、ありがとう! – Tiago

+0

私はXCodeの7.1にこれを貼り付けるカット/とき、それはコンパイルされませんし、 '&キー[I]'に「『CONSTボイド』不完全型へのポインタの添え字」と言うと、 '&' notificationCallbackの値[i]は '( ) '関数を呼び出す。 – Volomike