当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - postNotification trigger the function correctly , but userinfo(NSDictionary) has nothing

问题描述:

I used the codes below to notify the menu selection index

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

forKey:@"Index"];

[[NSNotificationCenter defaultCenter] postNotificationName: @"notifyToMenuSelectionNotification"

object: userInfo];

-(void)menuSelectionNotification:(NSNotification *)notification

{

NSLog(@"%@", notification.userInfo);

}

menuSelectionNotification is triggered correctly,

but NSLog output notification.userInfo is still {null}

Welcome any comment1

网友答案:

you are passing object in wrong way. Please try this -

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

[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyToMenuSelectionNotification"
                                      object:nil
                                      userInfo:userInfo];
网友答案:

You have to set your dictionary as the userInfo parameter, not as the object. You may want to try

[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyToMenuSelectionNotification" object:nil userInfo:userInfo];
网友答案:

You should post the dictionary in userInfo parameter , like

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

For more detail, pl. see the NSNotificationCenter documentation

网友答案:

please try this

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

-(void)menuSelectionNotification:(NSNotification *)notification{
NSDictionary *userInfo =   (NSDictionary*)notification.object;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: