3

所以我的目标是使用 using 将通知传递给另一个类NSNotificationCenter,我也想将object通知传递给 other class,我应该怎么做?

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 回答