对于两个不同的通知,我基本上有相同的代码。第一个是正确返回我的数据,但第二个是 nil。这可能是一个愚蠢的问题,但可能是因为我对两者都使用了相同的 NSNotificationCenter 吗?
NSNotificationCenter *note = [NSNotificationCenter defaultCenter];
[note addObserver:self selector:@selector(onProcessedReady:) name:@"Processed" object:nil];
[note addObserver:self selector:@selector(onGeneratedReady:) name:@"Generated" object:nil];
这是我为 NSNotification 回调设置数据的地方:
NSMutableDictionary *data = [NSMutableDictionary dictionaryWithCapacity:1];
[data setObject:self.templateData forKey:@"Template"];
NSNotificationCenter *templateNote = [NSNotificationCenter defaultCenter];
[templateNote postNotificationName:@"TemplateGenerated" object:nil userInfo:data];
<-- 数据在这里看起来不错。
这是回调:
-(void) onGeneratedReady:(NSNotification *)note /// <-- note is nil
{
if ([note.name isEqualToString:@"TemplateGenerated"])
{
NSDictionary *userData = note.userInfo;
TemplateData *templateData = [userData objectForKey:@"Template"];
NSLog(@"what is in userData?");
}
}
需要注意的是对象'self.templateData'是我创建的一个对象类。
@property (nonatomic, strong) TemplateData *templateData;
为了确保我的对象'self.templateData'不是问题,我尝试了以下方法,但回调中返回的注释仍然为零。
NSMutableDictionary *data = [NSMutableDictionary dictionaryWithCapacity:1];
// [data setObject:self.template forKey:@"Template"];
[data setObject:@"I am so happy" forKey:@"NotHappy"];
NSNotificationCenter *templateNote = [NSNotificationCenter defaultCenter];
[templateNote postNotificationName:@"TemplateGenerated" object:nil userInfo:data];
数据在这里看起来不错。我有一个键/值对
[0] = @"NotHappy" : @"I am so happy"
key = (__NSCFConstantString *) @"NotHappy"
value = (__NSCFConstantString *) @"I am so happy"
这是回调:
-(void) onGeneratedReady:(NSNotification *)note /// <-- note is nil
{
if ([note.name isEqualToString:@"TemplateGenerated"])
{
NSDictionary *userData = note.userInfo;
TemplateData *templateData = [userData objectForKey:@"Template"];
NSLog(@"what is in userData?");
}
}