-1

我尝试为另一个班级打电话通知

// 属于A类

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

从另一个类调用通知 // 它在 B 类中

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

2 回答 2

1

在 A 类中,将 self 添加为具有名称的通知的观察者

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

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

[[NSNotificationCenter defaultCenter] postNotificationName:YourOnDidFinishLaunchingNotificationName
                                                    object:nil];
于 2013-02-20T01:05:43.330 回答
1

在 B 类中,您应该像这样将 addObserver 添加到 B:

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

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

这是一个非常有用的例子NSNotification

于 2013-02-20T01:28:42.883 回答