在 NSNotificationCenter 中添加自定义 属性
Add custom property in NSNotificationCenter
总的来说,我是 Objective C 的新手。
我正在创建一个 class 来侦听通知
在该通知中,我打算将其作为具有两个键值对的 NSdictionary
// eventType -> determine which value to change
// value -> value which needs to be changed -> can be string, dynamic object and Bool
TS equivalent would
interface event {
eventType: string
value: any
}
在a.h,我目前有
- (void)initMessageEmit {
NSLog(@" Initialize observers ");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveNotification:)
name:@"selfParticipantNotification"
object:nil];
}
空 receive notification
- (void) receiveNotification:(NSNotification *) notification
{
我希望这样接收通知
- (void) receiveNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"selfParticipantNotification"]) {
NSDictionary *event = notification.event;
if (![event value]) {
if ([[event eventType] isEqualToString:@"enabled"]) {
[self enabled:event.value]
}
}
}
}
在上面,如果 eventType 值为 enabled
,我调用一个方法 enabled
并更新一个 属性 值。
value
这里可以是任何东西 -> NSString/NSDictionary/Bool 和 is/should 被 postNotificationName
传递
问题:如何使用 NSNotificationCenter 发送数据?
您可以使用 object
参数来 post 任何带有如下通知的内容。
[[NSNotificationCenter defaultCenter]
postNotificationName:@"selfParticipantNotification"
object:@{@"key":@"value"}];
收到通知时,您可以像下面这样读取这个值。
(void) receiveNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"selfParticipantNotification"]) {
NSDictionary *dictionary = (NSDictionary*)notification.object;
// Do something with dictionary
}
}
总的来说,我是 Objective C 的新手。
我正在创建一个 class 来侦听通知
在该通知中,我打算将其作为具有两个键值对的 NSdictionary
// eventType -> determine which value to change
// value -> value which needs to be changed -> can be string, dynamic object and Bool
TS equivalent would
interface event {
eventType: string
value: any
}
在a.h,我目前有
- (void)initMessageEmit {
NSLog(@" Initialize observers ");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveNotification:)
name:@"selfParticipantNotification"
object:nil];
}
空 receive notification
- (void) receiveNotification:(NSNotification *) notification
{
我希望这样接收通知
- (void) receiveNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"selfParticipantNotification"]) {
NSDictionary *event = notification.event;
if (![event value]) {
if ([[event eventType] isEqualToString:@"enabled"]) {
[self enabled:event.value]
}
}
}
}
在上面,如果 eventType 值为 enabled
,我调用一个方法 enabled
并更新一个 属性 值。
value
这里可以是任何东西 -> NSString/NSDictionary/Bool 和 is/should 被 postNotificationName
问题:如何使用 NSNotificationCenter 发送数据?
您可以使用 object
参数来 post 任何带有如下通知的内容。
[[NSNotificationCenter defaultCenter]
postNotificationName:@"selfParticipantNotification"
object:@{@"key":@"value"}];
收到通知时,您可以像下面这样读取这个值。
(void) receiveNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"selfParticipantNotification"]) {
NSDictionary *dictionary = (NSDictionary*)notification.object;
// Do something with dictionary
}
}