3

したがって、私の目標は、を使用して別のクラスに通知を配信することです。また、通知を他のクラスNSNotificationCenterに渡したいのですが、どうすればよいですか?objectclass

4

2 に答える 2

7

最初に通知名を登録する必要があります

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startLocating:) name:@"ForceUpdateLocation" object:nil]; // don't forget the ":"

そして、パラメータの辞書を含む通知を投稿します

[[NSNotificationCenter defaultCenter] postNotificationName:@"ForceUpdateLocation" object:self userInfo:[NSDictionary dictionaryWithObject:@"1,2,3,4,5" forKey:@"categories_ids"]]; 

そしてその方法は

- (void)startLocating:(NSNotification *)notification {

    NSDictionary *dict = [notification userInfo];
}
于 2011-07-24T22:18:40.620 に答える
0

ここで説明されているように、通知を投稿するための任意のメソッドを呼び出すだけです。たとえば、次のようになります。

通知を投稿するには:

-(void)postNotificationName:(NSString *)notificationName
                     object:(id)notificationSender
                   userInfo:(NSDictionary *)userInfo;

whereuserInfoは便利なオブジェクトを含む辞書です。

反対側で通知を登録するには:

-(void)addObserver:(id)notificationObserver
           selector:(SEL)notificationSelector
               name:(NSString *)notificationName
             object:(id)notificationSender;

Apple のNotification Programming Topicsも確認できます。

于 2011-07-24T22:15:42.010 に答える