0

as i told here i am using NSNotificationCenter .

on class A (observer) on the init method i have got :

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSensorsData:) name:@"HotSpotTouched" object:nil];

on classB i have got :

//FILL NSDICTIONARY WITH DATA
    [dict setObject:@"SPOT1" forKey:[array objectAtIndex:0]];
    [dict setObject:@"SPOT2" forKey:[array objectAtIndex:1]];
    [dict setObject:@"SPOT3" forKey:[array objectAtIndex:2]];
    [dict setObject:@"SPOT4" forKey:[array objectAtIndex:3]];
    [dict setObject:@"SPOT5" forKey:[array objectAtIndex:4]];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"HotSpotTouched" object:dict];

the function in class A getSensorsData is not being called.

whats wrong here ??

thanks !

Curnelious
  • 1
  • 16
  • 76
  • 150
  • what does your declaration for `getSensorsData` look like? is it in your `.h` interface file? – Michael Dautermann Feb 26 '12 at 16:16
  • @MichaelDautermann : -(void)getSensorsData:(NSNotification *)SPOTS – Curnelious Feb 26 '12 at 16:18
  • 1
    That declaration looks potentially wrong. Edit your question to show your declaration and how you're accessing your `NSDictionary` within `getSensorsData`. Are you setting a breakpoint and is it not hitting when the notification fires? Is classB happening on a different thread? – Michael Dautermann Feb 26 '12 at 16:22

3 Answers3

3

You are passing in dict as notificationSender when posting the notification, and nil when you add the observer. This way your notification is filtered out because the senders mismatch.

Update:
As pointed out by joerick in the comments, passing nil when adding an observer will disable sender filtering. So this isn't the problem here.

I just created a small sample project and for me notifications are delivered.

@Rant: If you want to pass arbitrary data along with your notification, you should use the userInfo dictionary (as pointed out by Cyrille in the comment).

Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
  • yeah, that's what I was thinking, too. – Michael Dautermann Feb 26 '12 at 16:26
  • 2
    You want to post `dict` as the `userData`, not the `object` of your `NSNotification`. – Cyrille Feb 26 '12 at 16:27
  • 1
    passing `nil` for `addObserver:...` means all notifications are observed, regardless of sender. https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html – joerick Feb 26 '12 at 16:29
  • couldnt understand you, so i have to change nil to dict ? but the other class dont know dict.. – Curnelious Feb 26 '12 at 16:31
  • @weichsel i have change to: [[NSNotificationCenter defaultCenter] postNotificationName:@"HotSpotTouched" object:self userInfo:dict]; still no luck – Curnelious Feb 26 '12 at 16:45
  • i have seen that the fact that i put oserver on the init, its the problem and that i have to put it on the initWithStyle ??? – Curnelious Feb 26 '12 at 17:14
3

The calls to the notification center look correct. I suspect the problem is due to the liffe cycle of object A. You say that you're registering for the notification in the init method. Have you correctly assigned self?:

-(id)init
{
    //self does not have a meaningful value prior to the call to [super init]
    self = [super init];
    if (self != nil)
    {
        //ensure addObserver is called in the if code block
    }
    return self;
}

Also, it's good practice to use constants for notification names as they mitigate against typos. See Constants in Objective C.

Community
  • 1
  • 1
Benedict Cohen
  • 11,912
  • 7
  • 55
  • 67
-1

problem solved:

if you passing a null argument, the observer is not getting the call !

my NSDictionary argument was null( because a reason i still dont know), so the call is not fired.

Venk
  • 5,949
  • 9
  • 41
  • 52
Curnelious
  • 1
  • 16
  • 76
  • 150