How to send a notification with parameters on Objective-C?
Solution 1
Use postNotificationName:object:userInfo:
and bundle any parameter you wish to pass inside the userInfo
dictionary.
Example:
You can post a notification like this
NSDictionary * userInfo = @{ @"toOrientation" : @(toOrientation) };
[[NSNotificationCenter defaultCenter] postNotificationName:@"willAnimateRotationToInterfaceOrientation" object:nil userInfo:userInfo];
And then retrieve the information you passed, by doing:
- (void)willAnimateRotationToInterfaceOrientation:(NSNotification *)n {
UIInterfaceOrientation toOrientation = (UIInterfaceOrientation)[n.userInfo[@"toOrientation"] intValue];
//..
}
To summarize what seen above, the selector used to handle a notification takes one optional parameter of type NSNotification
and you can store any information you'd like to pass around inside the userInfo
dictionary.
Solution 2
This doesn't work the way you think it does. A notification message call has one optional parameter, which is an NSNotification
object:
-(void)myNotificationSelector:(NSNotification*)note;
-(void)myNotificationSelector;
The notification object has a property, userInfo
, which is a dictionary that can be used to pass relevant information. But you can't register arbitrary selectors to be called by the notification center. You pass that dictionary with the notification by using -postNotificationName:object:userInfo:
instead of -postNotificationName:object:
; the userInfo
parameter is just an NSDictionary
that you create.
Dmitry
Updated on June 17, 2022Comments
-
Dmitry almost 2 years
I need to send the notification
@"willAnimateRotationToInterfaceOrientation"
with the parameterstoInterfaceOrientation
andduration
(question #1) to allUIViewController
on the application (question #2). How to write code for that?[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willAnimateRotationToInterfaceOrientation:toInterfaceOrientation:duration) name:@"willAnimateRotationToInterfaceOrientation" object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:@"willAnimateRotationToInterfaceOrientation" object:self];
-
Dmitry about 11 yearsBut how to pass the parameters using
userInfo
? -
Dmitry about 11 yearsThanks, but is it possible to send a notification to all (or all visible)
UIViewController
s of the application? -
Seamus Campbell about 11 yearsOnly if they've all registered for that notification. Note that there is an existing notification,
UIDeviceOrientationDidChangeNotification
that is available if you ask for it by calling[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]
(but you still must register each view controller for it). -
Dmitry about 11 years
UIDeviceOrientationDidChangeNotification
is calling after a rotation - not inside the rotation aswillAnimateRotationToInterfaceOrientation
. Sorry for possible noob question, but we are callingpostNotificationName
withobject:self
parameter - will it be sent only toself
object? -
Seamus Campbell about 11 yearsIt will be sent to all objects that passed that pointer to the
object
parameter when they calledNSNotificationCenter -addObserver:selector:name:object:
. -
Dmitry about 11 yearsCode has the following errors: 1. Unexpected '@' in program. 2. Collection element of type 'UIInterfaceOrientation' (aka 'enum UIInterfaceOrientation') is not an Objective-C object.
-
Dmitry about 11 yearsMay I use
object:nil
onaddObserver:selector:name:object:
and onpostNotificationName:object:
both to send the notification to all subscribers? -
Seamus Campbell about 11 yearsAbsolutely. In fact, you only have to pass
nil
inaddObserver:::
; you'll then get all the notifications, even ifpostNotificationName:object:
has a non-nil
value forobject
. -
Dmitry about 11 yearsThank you a lot! May you also help to pass
UIInterfaceOrientation toInterfaceOrientation
andNSTimeInterval duration
parameters with the notification? -
Gabriele Petronella about 11 yearsYou need to wrap
toOrientation
between parenthesis. Fixed. -
Seamus Campbell about 11 yearsSee Gabriele's code. You need to push both values into
NSNumber
s, becauseNSDictionary
can only hold object values.