我想在发生某些事件时向观察者发送一些通知.而且我还想知道观察者如何捕获/处理/接收该通知? [英] i want to send some notifications to observers when some event occurs. and i also want to know how observer catch/handle/receive that notification?

查看:49
本文介绍了我想在发生某些事件时向观察者发送一些通知.而且我还想知道观察者如何捕获/处理/接收该通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读可可基本指南时,我经历了:

while reading cocoa fundamental guide i have gone through:

  • (void)addObserver:(id)notificationObserver选择器:(SEL)notificationSelector名称:(NSString *)notificationName对象:(id)notificationSender

我正在掌握所有理论,但实际上我正在寻找使用它的真实示例?

I am getting all theories, but actually i am looking for a real example where it is used ?

任何人都可以提供示例示例.

Can any one give some sample example.

实际上,我想在发生某些事件时向观察者发送一些通知.

Actually i want to send some notifications to observers when some event occurs.

我还想知道观察者如何捕获/处理/接收该通知?

and i also want to know how observer catch/handle/receive that notification ??

谢谢

推荐答案

要侦听通知,请在要接收通知的实例中添加与此类似的代码:

To listen for notifications, add code similar to this in the instance that you want to receive notifications:

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

当您要发布通知时:

[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"SOME_NOTIFICATION_NAME" object:nil]];

在处理应用程序内购买时,我使用与此类似的代码来更新我的UI.

I use code similar to this to update my UI while processing an in app purchase.

这篇关于我想在发生某些事件时向观察者发送一些通知.而且我还想知道观察者如何捕获/处理/接收该通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆