通过Objective-C中的NSNotificationCenter发送和接收消息?


610

我正在尝试通过NSNotificationCenterObjective-C 发送和接收消息。但是,我尚未找到有关如何执行此操作的任何示例。您如何通过发送和接收消息NSNotificationCenter


真的非常有用,谢谢。一件事,addObserver方法在指定的选择器之后不应该在结尾的分号(至少在我的版本中引起了异常)。我尝试编辑上面的代码,但是由于原始代码中的格式问题,更改未被接受。
Braunius


2
这个q太基础和广泛了,有点谷歌应该会很好
Daij-Djan 2012年

这是非常相似的一个相关的问题在这里:stackoverflow.com/questions/7896646/...
大卫道格拉斯

55
我发现这样的问题很荒谬,因为当Stack Overflow的用户如此清楚地评论了它的用处时,这样的闭合是没有建设性的
Chet 2014年

Answers:


1019
@implementation TestClass

- (void) dealloc
{
    // If you don't remove yourself as an observer, the Notification Center
    // will continue to try and send notification objects to the deallocated
    // object.
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (id) init
{
    self = [super init];
    if (!self) return nil;

    // Add this instance of TestClass as an observer of the TestNotification.
    // We tell the notification center to inform us of "TestNotification"
    // notifications using the receiveTestNotification: selector. By
    // specifying object:nil, we tell the notification center that we are not
    // interested in who posted the notification. If you provided an actual
    // object rather than nil, the notification center will only notify you
    // when the notification was posted by that particular object.

    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(receiveTestNotification:) 
        name:@"TestNotification"
        object:nil];

    return self;
}

- (void) receiveTestNotification:(NSNotification *) notification
{
    // [notification name] should always be @"TestNotification"
    // unless you use this method for observation of other notifications
    // as well.

    if ([[notification name] isEqualToString:@"TestNotification"])
        NSLog (@"Successfully received the test notification!");
}

@end

...在另一个班级的其他地方...

- (void) someMethod
{

    // All instances of TestClass will be notified
    [[NSNotificationCenter defaultCenter] 
        postNotificationName:@"TestNotification" 
        object:self];

}

2
只是想知道[NSNotificationCenter defaultCenter]的放置位置。最好将其放在您的AppDelegate中吗?
fulvio

14
@Fulvio:这取决于是否接收或发布可能影响应用程序所有部分的通知,请将其放入AppDelegate中。如果您接收/发布仅影响单个班级的通知,请改为将其放在该班级中。
dreamlax 2011年

1
@dreamlax真相,但是,值得注意的是,这个问题主要是由新的ios开发人员搜索的,他们使通知侦听器的生存期超过了他们的需要。现在,使用arc时,您通常不使用dealloc,因此某些人可能认为他们不必释放监听器。
Vive

7
值得一提的[super dealloc]是,ARC不允许在dealloc-method 中进行调用;其余的一切都很好。
tommys 2014年

1
如果通知触发并且没有观察者,会发生什么?通知丢失了吗?还是将其“保存”到准备运送给新观察者的位置(稍后创建)?
superpuccio

226

扩展dreamlax的示例 ...如果要与通知一起发送数据

在发布代码中:

NSDictionary *userInfo = 
[NSDictionary dictionaryWithObject:myObject forKey:@"someKey"];
[[NSNotificationCenter defaultCenter] postNotificationName: 
                       @"TestNotification" object:nil userInfo:userInfo];

在观察代码中:

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

    NSDictionary *userInfo = notification.userInfo;
    MyObject *myObject = [userInfo objectForKey:@"someKey"];
}

TestNotification必须为NSString类型。它是实例变量NSNotification吗?
RomanHouse'5

1
我可以使用selfreceiveTestNotification方法访问观察者吗?
为什么

为什么是。receiveTestNotification是一个实例方法,您可以通过其中的self来访问实例本身。
2014年

而已。我一直在寻找一种从接收器方法获取UserInfo的方法。
哈桑2015年

似乎所有观察者的想法并未涵盖所有情况。该应用程序无法正常运行。已关闭,并且通知中心的通知表被窃听。观察者方法不会被调用。
哈桑2015年

49

这帮助了我:

// Add an observer that will respond to loginComplete
[[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(showMainMenu:) 
                                                 name:@"loginComplete" object:nil];


// Post a notification to loginComplete
[[NSNotificationCenter defaultCenter] postNotificationName:@"loginComplete" object:nil];


// the function specified in the same class where we defined the addObserver
- (void)showMainMenu:(NSNotification *)note {
    NSLog(@"Received Notification - Someone seems to have logged in"); 
}

来源:http : //www.smipple.net/snippet/Sounden/Simple%20NSNotificationCenter%20example


那对我有用!谢谢
Rakshitha Muranga Rodrigo

48

也可以使用块:

NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[[NSNotificationCenter defaultCenter] 
     addObserverForName:@"notificationName" 
     object:nil
     queue:mainQueue
     usingBlock:^(NSNotification *notification)
     {
          NSLog(@"Notification received!");
          NSDictionary *userInfo = notification.userInfo;

          // ...
     }];

苹果的文档


1
这是对我的答案的很好的更新,我的答案现在已经过时了。通过引入或ARC和块,通知中心变得更容易处理。
dreamlax

5
我也这么认为,但事实证明它太好了,难以置信。在这种情况下,您必须保留addObserver返回的观察者,然后再删除该观察者,这使其与创建新方法一样复杂,甚至更多。更多信息:toastmo.com/blog/2012/12/04/…–
Andrew

42

如果您使用NSNotificationCenter更新视图,请不要忘记通过调用从主线程发送视图dispatch_async

dispatch_async(dispatch_get_main_queue(),^{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"my_notification" object:nil];
});

1
是需要从主线程发出的通知帖子,还是刚在您实际更新视图时(即在接收发送到主线程的通知的方法内部)发出的帖子?
Crashalot '16

1
您从中发送通知的线程是运行函数的线程,因此试图更改UI。您也可以使用对函数内部主线程的调度,就像您说的那样:D。应该有相同的结果,所以感觉更好:D
eiran '16

1
@eiran,谢谢你这么多兄弟,它只是工作后,我写了里面dispatch_async
艾尔沙德Shaik

2

新手选择答案的SWIFT 5.1

class TestClass {
    deinit {
        // If you don't remove yourself as an observer, the Notification Center
        // will continue to try and send notification objects to the deallocated
        // object.
        NotificationCenter.default.removeObserver(self)
    }

    init() {
        super.init()

        // Add this instance of TestClass as an observer of the TestNotification.
        // We tell the notification center to inform us of "TestNotification"
        // notifications using the receiveTestNotification: selector. By
        // specifying object:nil, we tell the notification center that we are not
        // interested in who posted the notification. If you provided an actual
        // object rather than nil, the notification center will only notify you
        // when the notification was posted by that particular object.

        NotificationCenter.default.addObserver(self, selector: #selector(receiveTest(_:)), name: NSNotification.Name("TestNotification"), object: nil)
    }

    @objc func receiveTest(_ notification: Notification?) {
        // [notification name] should always be @"TestNotification"
        // unless you use this method for observation of other notifications
        // as well.

        if notification?.name.isEqual(toString: "TestNotification") != nil {
            print("Successfully received the test notification!")
        }
    }
}

...在另一个班级的其他地方...

 func someMethod(){
        // All instances of TestClass will be notified
        NotificationCenter.default.post(name: "TestNotification", object: self)
 }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.