如何从另一个类调用通知

时间:2013-02-20 00:56:30

标签: iphone ios

我尝试为另一个类调用通知

//哪个在Class

- (void)onDidFinishLaunchingNotification:(NSNotification*)notification
{
    NSLOG(@"onDidFinishLaunchingNotification");
}

从另一个类调用通知 //哪个在B组

 [[NSNotificationCenter defaultCenter]addObserver:nil selector:@selector(onDidFinishLaunchingNotification:) name:nil object:nil];

2 个答案:

答案 0 :(得分:1)

在A类中,使用名称

添加self作为通知的观察者
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(onDidFinishLaunchingNotification:)
                                             name:YourOnDidFinishLaunchingNotificationName
                                           object:nil];

在B类中,使用-postNotificationName:object:发布通知:

[[NSNotificationCenter defaultCenter] postNotificationName:YourOnDidFinishLaunchingNotificationName
                                                    object:nil];

答案 1 :(得分:1)

在B组中你可以像这样将addObserver转发给B:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(onDidFinishLaunchingNotification:)
                                             name:YourOnDidFinishLaunchingNotificationName
                                           object:nil];

我认为您应该查看addObserver:selector:name:object:

的文档

这里使用NSNotification

非常useful example