开发者

access object passed in NSNotification?

开发者 https://www.devze.com 2023-03-21 03:31 出处:网络
I have a NSNotification that is posting a NSDictionary: NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:

I have a NSNotification that is posting a NSDictionary:

 NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                          anItemID, @"ItemID",
                                          [NSString stringWithFormat:@"%i",q], @"Quantity",
                                          [NSString stringWithFormat:@"%@",[NSDate date]], @"BackOrderDate",
                                          [NSString stringWithFormat:@"%@", [NSDate date]],@"ModifiedOn",
                                          nil];

                    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"InventoryUpdate" object:dict]];

How do I subscribe to this and get information from this NSDictionary?

in my viewDidLoad I have:

[[NSNotificationCenter defaultCenter] addObserver开发者_运维知识库:self selector:@selector(recieveInventoryUpdate:) name:@"InventoryUpdate" object:nil];

and a method in the class:

- (void)recieveInventoryUpdate:(NSNotification *)notification {
    NSLog(@"%@ updated", [notification userInfo]);
}

which logs a null value of course.


it's [notification object]

you can also send userinfo by using notificationWithName:object:userInfo: method


Object is what object is posting the notification, not a way to store the object so you can get to it. The user info is where you store information you want to keep with the notification.

[[NSNotificationCenter defaultCenter] postNotificationName:@"Inventory Update" object:self userInfo:dict];

Then register for the notification. The object can be your class, or nil to just receive all notifications of this name

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

Next use it in your selector

- (void)recieveInventoryUpdate:(NSNotification *)notification {
    NSLog(@"%@ updated", [notification userInfo]);
}


It's simple, see below

- (void)recieveInventoryUpdate:(NSNotification *)notification {
    NSLog(@"%@ updated",notification.object); // gives your dictionary 
    NSLog(@"%@ updated",notification.name); // gives keyname of notification

}

if access the notification.userinfo, it will return null.


You are doing it wrong. You need to use:

-(id)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)userInfo

and pass the dict to the last parameter. Your "object" parameter is the object sending the notification and not the dictionary.


Swift:

// Propagate notification:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: ["info":"your dictionary"])

// Subscribe to notification:
NotificationCenter.default.addObserver(self, selector: #selector(yourSelector(notification:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)

// Your selector:
func yourSelector(notification: NSNotification) {
    if let info = notification.userInfo, let infoDescription = info["info"] as? String {
            print(infoDescription)
        } 
}

// Memory cleaning, add this to the subscribed observer class:
deinit {
    NotificationCenter.default.removeObserver(self)
}


The object from the notification is intended to be the sender, in your case the dictionary is not actually the sender, its just information. Any auxiliary information to be sent along with the notification is intended to be passed along with the userInfo dictionary. Send the notification as such:

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                      anItemID, 
                                      @"ItemID",
                                      [NSString stringWithFormat:@"%i",q], 
                                      @"Quantity",
                                      [NSString stringWithFormat:@"%@", [NSDate date]], 
                                      @"BackOrderDate",
                                      [NSString stringWithFormat:@"%@", [NSDate date]],
                                      @"ModifiedOn",
                                      nil];

[[NSNotificationCenter defaultCenter] postNotification:
        [NSNotification notificationWithName:@"InventoryUpdate" 
                                      object:self 
                                    userInfo:dict]];

And then receive it like this, to get the behavior you intend in a good way:

- (void)recieveInventoryUpdate:(NSNotification *)notification {
    NSLog(@"%@ updated", [notification userInfo]);
}


More simpler way is

-(void)recieveInventoryUpdate:(NSNotification *)notification
{
    NSLog(@"%@ updated",[notification object]);
    //Or use notification.object
}

It worked for me.


Simple Answer

  • use postNotificationName:object:userInfo:'s userInfo to pass data (your dict)
    • NOT (mis)use postNotificationName:object:'s object
      • for object is the sender=The object which posting the notification

Detail Answer

full code should be:

definition

  • observer in your viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recieveInventoryUpdate:) name:@"InventoryUpdate" object:nil];
  • and handle notification method
- (void)recieveInventoryUpdate:(NSNotification *)notification {
    NSLog(@"notification=%@", notification);
    // NSDictionary* yourPassedInDict = notification.userInfo;
    NSDictionary* yourPassedInDict = [notification userInfo];
    NSLog(@"dict=%@", yourPassedInDict);
}

usage = caller

  • posting a NSDictionary
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
    anItemID, @"ItemID",
    [NSString stringWithFormat:@"%i",q], @"Quantity",
    [NSString stringWithFormat:@"%@",[NSDate date]], @"BackOrderDate",
    [NSString stringWithFormat:@"%@", [NSDate date]],@"ModifiedOn",
    nil];

[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"InventoryUpdate" object:self userInfo:dict]];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号