2

我使用下面的代码来通知菜单选择索引

    NSDictionary *userInfo=  [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"123"] 
                                              forKey:@"Index"];
   [[NSNotificationCenter defaultCenter] postNotificationName: @"notifyToMenuSelectionNotification"
                                                    object: userInfo];



-(void)menuSelectionNotification:(NSNotification *)notification
{
  NSLog(@"%@", notification.userInfo);


}

menuSelectionNotification 被正确触发,

但 NSLog 输出 notification.userInfo 仍然是 {null}

欢迎任何评论1

4

4 回答 4

4

你以错误的方式传递对象。请试试这个 -

NSDictionary *userInfo=  [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"123"] 
                                              forKey:@"Index"];

[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyToMenuSelectionNotification"
                                      object:nil
                                      userInfo:userInfo];
于 2012-04-12T07:56:25.070 回答
0

您必须将字典设置为 userInfo 参数,而不是对象。你可能想试试

[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyToMenuSelectionNotification" object:nil userInfo:userInfo];
于 2012-04-12T07:52:50.360 回答
0

userInfo您应该在参数中发布字典,例如

[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyToMenuSelectionNotification" object:nil userInfo:userInfo];

有关更多详细信息,请。请参阅NSNotificationCenter文档

于 2012-04-12T07:59:30.950 回答
0

请试试这个

[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyToMenuSelectionNotification" object:userInfo userInfo:nil];

-(void)menuSelectionNotification:(NSNotification *)notification{
NSDictionary *userInfo =   (NSDictionary*)notification.object;
}
于 2012-04-12T08:32:24.273 回答