如何使用postNotificationName:object传递NSDictionary:

use*_*859 38 iphone xcode nsdictionary nsnotificationcenter

我试图使用NSNotificationCenter将NSDictionary表单UIView传递给UIViewController.字典在发布通知时工作正常,但在接收方法中,我无法访问字典中的任何对象.

这是我如何创建字典并发布通知...

itemDetails = [[NSDictionary alloc] initWithObjectsAndKeys:@"Topic 1", @"HelpTopic", nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"HotSpotTouched" object:itemDetails];
Run Code Online (Sandbox Code Playgroud)

在UIViewController中,我正在设置观察者......

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(hotSpotMore:)
                                             name:@"HotSpotTouched"
                                           object:nil];
Run Code Online (Sandbox Code Playgroud)

出于测试目的,hotSpotMore看起来像这样......

- (void)hotSpotMore:(NSDictionary *)itemDetails{
      NSLog(@"%@", itemDetails);
      NSLog(@"%@", [itemDetails objectForKey:@"HelpTopic"]);    
}
Run Code Online (Sandbox Code Playgroud)

第一个NSLog工作正常,显示字典的内容.第二个日志抛出以下异常......

 [NSConcreteNotification objectForKey:]: unrecognized selector sent to instance 0x712b130
Run Code Online (Sandbox Code Playgroud)

我不明白为什么我无法访问传递的字典中的任何对象.

在此先感谢您的帮助.

约翰

Mat*_*uch 110

第一个NSLog工作正常,显示字典的内容.第二个日志抛出以下异常......

该程序试图欺骗你,它看起来就像是你的字典,因为你的字典在通知中.从异常中可以看出,您的对象实际上来自名为NSConcreteNotification的类.
这是因为notification-method的参数始终是NSNotification-object.这将工作:

- (void)hotSpotMore:(NSNotification *)notification {
      NSLog(@"%@", notification.object);
      NSLog(@"%@", [notification.object objectForKey:@"HelpTopic"]);    
}
Run Code Online (Sandbox Code Playgroud)

就像一个提示:对象通常是发送通知的对象,您应该将您的NSDictionary作为userInfo发送.
我认为如果您这样做会改善您的代码:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotificationName:@"HotSpotTouched" object:self userInfo:itemDetails];


- (void)hotSpotMore:(NSNotification *)notification {
      NSLog(@"%@", notification.userInfo);
      NSLog(@"%@", [notification.userInfo objectForKey:@"HelpTopic"]);    
}
Run Code Online (Sandbox Code Playgroud)

object参数用于区分可以发送通知的不同对象.
假设您有两个不同的HotSpot对象,它们都可以发送通知.设置objectin时addObserver:selector:name:object:,可以为每个对象添加不同的观察者.使用nil作为对象参数意味着应该接收所有通知,而不管发送通知的对象如何.

例如:

FancyHotSpot *hotSpotA;
FancyHotSpot *hotSpotB;

// notifications from hotSpotA should call hotSpotATouched:
[[NSNotificationCenter defaultCenter] addObserver:self 
       selector:@selector(hotSpotATouched:) name:@"HotSpotTouched" 
       object:hotSpotA]; // only notifications from hotSpotA will be received

// notifications from hotSpotB should call hotSpotBTouched:
[[NSNotificationCenter defaultCenter] addObserver:self 
       selector:@selector(hotSpotBTouched:) name:@"HotSpotTouched" 
       object:hotSpotB]; // only notifications from hotSpotB will be received

// notifications from all objects should call anyHotSpotTouched:
[[NSNotificationCenter defaultCenter] addObserver:self 
       selector:@selector(anyHotSpotTouched:) name:@"HotSpotTouched" 
       object:nil]; // nil == “any object”, so all notifications with the name “HotSpotTouched” will be received


- (void)hotSpotATouched:(NSNotification *)n {
    // only gets notification of hot spot A
}

- (void)hotSpotBTouched:(NSNotification *)n {
    // only gets notification of hot spot B
}

- (void)anyHotSpotTouched:(NSNotification *)n {
    // catches all notifications
}
Run Code Online (Sandbox Code Playgroud)

  • +1程序变得有感觉并试图欺骗你:) (8认同)