views:

63

answers:

2

Hi, I am trying to send some data using NSNotification but get stuck. Here is my code:

// Posting Notification
NSDictionary *orientationData;
if(iFromInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    orientationData = [NSDictionary dictionaryWithObject:@"Right"
                                                  forKey:@"Orientation"];
}

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter postNotificationName:@"Abhinav"
                                  object:nil
                                userInfo:orientationData];

// Adding observer
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(orientationChanged)
                                             name:@"Abhinav"
                                           object:nil];

Now how to fetch this userInfo dictionary in my selector orientationChanged?

+2  A: 

You get an NSNotification object passed to your function. This includes the name, object and user info that you provided to the NSNotificationCenter.

- (void)orientationChanged:(NSNotification *)notification
{
    NSDictionary *dict = [notification userInfo];
}
JustSid
Ok. And how to do this? Is it like: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:self) name:@"Abhinav" object:nil];?
Abhinav
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"Abhinav" object:nil];
JustSid
+1  A: 

Your selector must have ":" to accept parameters. e.g. @selector(orientationChanged:) then in the method declaration it can accept the NSNotification parameter.

Manny