How to send a notification with parameters on Objective-C?

15,704

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.

Share:
15,704
Dmitry
Author by

Dmitry

Updated on June 17, 2022

Comments

  • Dmitry
    Dmitry almost 2 years

    I need to send the notification @"willAnimateRotationToInterfaceOrientation" with the parameters toInterfaceOrientation and duration (question #1) to all UIViewController 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
    Dmitry about 11 years
    But how to pass the parameters using userInfo?
  • Dmitry
    Dmitry about 11 years
    Thanks, but is it possible to send a notification to all (or all visible) UIViewControllers of the application?
  • Seamus Campbell
    Seamus Campbell about 11 years
    Only 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
    Dmitry about 11 years
    UIDeviceOrientationDidChangeNotification is calling after a rotation - not inside the rotation as willAnimateRotationToInterfaceOrientation. Sorry for possible noob question, but we are calling postNotificationName with object:self parameter - will it be sent only to self object?
  • Seamus Campbell
    Seamus Campbell about 11 years
    It will be sent to all objects that passed that pointer to the object parameter when they called NSNotificationCenter -addObserver:selector:name:object:.
  • Dmitry
    Dmitry about 11 years
    Code has the following errors: 1. Unexpected '@' in program. 2. Collection element of type 'UIInterfaceOrientation' (aka 'enum UIInterfaceOrientation') is not an Objective-C object.
  • Dmitry
    Dmitry about 11 years
    May I use object:nil on addObserver:selector:name:object: and on postNotificationName:object: both to send the notification to all subscribers?
  • Seamus Campbell
    Seamus Campbell about 11 years
    Absolutely. In fact, you only have to pass nil in addObserver:::; you'll then get all the notifications, even if postNotificationName:object: has a non-nil value for object.
  • Dmitry
    Dmitry about 11 years
    Thank you a lot! May you also help to pass UIInterfaceOrientation toInterfaceOrientation and NSTimeInterval duration parameters with the notification?
  • Gabriele Petronella
    Gabriele Petronella about 11 years
    You need to wrap toOrientation between parenthesis. Fixed.
  • Seamus Campbell
    Seamus Campbell about 11 years
    See Gabriele's code. You need to push both values into NSNumbers, because NSDictionary can only hold object values.