如何创建一个类以通过Objective-C中的NSNotificationCenter发送和接收事件? [英] How to create a class to send and receive events through NSNotificationCenter in Objective-C?

查看:88
本文介绍了如何创建一个类以通过Objective-C中的NSNotificationCenter发送和接收事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建两个类,并且两个类都应该能够通过NSNotificationCenter方法发送和接收事件.即,两个都应该具有sendEvent和receiveEvent方法:

I need to create two classes and both should be able to send and receive the events through the NSNotificationCenter methods.ie Both should have the sendEvent and receiveEvent methods:

      @implementation Class A
-(void)sendEvent
{
    addObserver:---  name:---- object:---
}

-(void)ReceiveEvent
{
postNotificationName: --- object:---
}
@end

与另一个类一样,ClassB也应该能够发送和接收事件.怎么办?

Same as such another class say ClassB should also be able to send and receive the events. How can it be done?

推荐答案

理想情况下,对象在初始化后便会开始观察有趣的事件.因此它将在NotificationCenter的初始化代码中注册所有有趣的事件. sendEvent:基本上是postNotification:方法的包装.

Ideally an object would start observing interesting events as soon as its initialized. So it will register all interesting events with the NotificationCenter inside its initialization code. sendEvent: is basically a wrapper around the postNotification: method.

@implementation A

- (id)init {
    if(self = [super init]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveEvent:) name:@"SomeEvent" object:nil];
    }
    return self;
}

- (void)sendEvent {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SomeOtherEvent" object:nil];
}

// Called whenever an event named "SomeEvent" is fired, from any object.
- (void)receiveEvent:(NSNotification *)notification {
    // handle event
}

@end

与B类相同.

您可能使问题复杂化了. NSNotificationCenter充当向其发送所有事件的代理,并决定将其转发给谁.就像观察者模式一样,但是对象不会直接观察或通知对方,而是通过中央经纪人-在这种情况下为NSNotificationCenter.这样,您就不需要直接连接两个可能通过#include进行交互的类.

You might be over-complicating the problem. A NSNotificationCenter acts as a broker to whom all events are sent and it decides who to forward that to. It's like the Observer pattern but objects don't directly observe or notify each other, but rather through a central broker - the NSNotificationCenter in this case. With that you don't need to directly connect two classes that might be interacting with each other with an #include.

在设计类时,不必担心对象将如何得到通知或如何将其通知其他感兴趣的对象,而不必担心对象在发生某些事件时需要得到通知,或者它需要将以下事件通知给NSNotficationCenter:它发生的事件.

While designing your classes, don't worry about how an object would get notified or how it would notify other interested objects, only that an object needs to get notified about some events when they occur, or it needs to inform NSNotficationCenter of its events when they occur.

因此,简而言之,找出对象应了解的所有事件,并在此init()方法中注册这些事件,然后在dealloc()方法中注销它们.

So in short, find out all events that an object should know about and register those events in this init() method, and unregister them in the dealloc() method.

您可能会发现基本教程很有帮助.

You may find this basic tutorial helpful.

这篇关于如何创建一个类以通过Objective-C中的NSNotificationCenter发送和接收事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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