NSNotification addObserver:someOtherClass

chi*_*lor 4 iphone

我需要将一条消息传递给一个控制类(它会创建一个将要发送消息的类的实例)所以,我不能直接在我的文件中引用类名,而不是让它成为一个全局(这是荒谬的如果"NSNotification"广告宣传能够传递各种消息,无论他们在哪里/什么类.

所以没有进一步的...

(从Say ClassB打电话)

ClassA创建ClassB的实例

现在ClassB,我需要传递有关按钮按下的消息ClassA

(insdie ClassB)
- (void)viewDidLoad
{
  [[NSNotificationCenter defaultCenter] addObserver:ClassA 
                                        selector:@selector(doLoginAction)      
                                        name:@"SomeButton" 
                                        object:nil];
  [super viewDidLoad];
}
Run Code Online (Sandbox Code Playgroud)

这不会编译,即使我包括,对不起,"#import "ClassA.h" 现在,如果我做一些愚蠢的事情,

ClassA *classa = [[ClassA alloc]init];
Run Code Online (Sandbox Code Playgroud)

然后使用这个新创建的classa实例addObserver:classa将编译,但正如我所想,绝对不会做任何事情......(我知道,但令人惊讶的是这种代码在Iphone编程书籍中很流行......)所以我试过了无论如何.

但是,如果我将此函数放入ClassA并使用addObserver:ClassB 它将被调用,但会导致堆栈转储unrecognized selector sent to instance 或使用addObserver:self.

我很想删除Xcode并回到vim并使用旧的"C"回调...

Ale*_*lik 9

所以,如果我做对了,你有一个ClassA创建实例ClassB.反过来,这些实例应直接发送通知,ClassA而不知道任何相关信息.

如果这是正确的,那么NSNotificationCenter正是您所需要的.

ClassA实现中添加如下initialialize方法:

@implementation ClassA

+ (void)initialize
{
    [[NSNotificationCenter defaultCenter]
        addObserver:self
           selector:@selector(YourSelector:)
               name:@"YourNotificationName"
             object:nil];
}

+ (void)YourSelector:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];

    // ...
}

// ...

@end
Run Code Online (Sandbox Code Playgroud)

然后,实例ClassB应仅使用其名称发布通知:

@implementation ClassB

- (void)postNotification
{
    NSDictionary *userInfo = ...;    // may be nil
    [[NSNotificationCenter defaultCenter]
        postNotificationName:@"YourNotificationName"
                               // the same name is used in the `addObserver` call
                               // in the previous code snippet
                      object:nil
                    userInfo:userInfo];
}

// ...

@end
Run Code Online (Sandbox Code Playgroud)

总而言之,如果您使用的是NSNotificationCenter,则无需了解有关通知接收方的任何信息.实际上,您可以订阅任意数量的对象以接收相同的通知,并且每个addObserver对象在接收到通知对象时将调用其适当的方法(指定).

请记住,在您将类实例添加为观察者而不是类对象本身的情况下,您应该调用[[NSNotificationCenter defaultCenter] removeObserver:self]该实例的dealloc方法.