How to pass userInfo to NSNotification?

I am trying to send some data using NSNotification but it is 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 get this userInfo dictionary in my Changed selection orientation?

+46
iphone cocoa-touch cocoa
Oct 05 '10 at 7:07
source share
4 answers

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

 - (void)orientationChanged:(NSNotification *)notification { NSDictionary *dict = [notification userInfo]; } 
+91
Oct 05 '10 at 7:15
source share

Your selector must have : to accept parameters.
eg.

 @selector(orientationChanged:) 

then in the method declaration it can accept the NSNotification parameter.

+23
05 Oct '10 at 7:21
source share

You are sending the notification correctly. Please change the Notification Browser as shown below.

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"Abhinav" object:nil]; - (void)orientationChanged:(NSNotification *)notification { NSDictionary *dict = [notification userInfo]; } 

Hope this solution will work for you.

+4
Mar 21 '13 at 5:04 on
source share

In quick To get userinfo object

  let dict = notification.userInfo print(dict) 
0
Apr 6 '16 at 10:11
source share



All Articles