在NSNotification中传递的访问对象?

问题描述 投票:23回答:7

我有一个发布NSDictionary的NSNotification:

 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]];

我如何订阅此并从此NSDictionary获取信息?

在我的viewDidLoad我有:

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

和班上的方法:

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

当然记录一个空值。

ios objective-c nsnotifications
7个回答
34
投票

这是[notification object]

您也可以使用notificationWithName:object:userInfo:方法发送userinfo


14
投票

对象是发布通知的对象,而不是存储对象的方法,因此您可以访问它。用户信息是您存储要与通知保持一致的信息的位置。

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

然后注册通知。对象可以是您的类,也可以只接收此名称的所有通知

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

接下来在选择器中使用它

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

3
投票

这很简单,见下文

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

}

如果访问notification.userinfo,它将返回null


2
投票

你做错了。你需要使用:

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

并将dict传递给最后一个参数。您的“对象”参数是发送通知而不是字典的对象。


1
投票

来自通知的object旨在成为发件人,在您的情况下,字典实际上不是发件人,它只是信息。与通知一起发送的任何辅助信息都将与userInfo字典一起传递。发送通知如下:

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]];

然后像这样接收它,以便以一种好的方式获得你想要的行为:

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

0
投票

迅速:

// 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)
}

0
投票

更简单的方法是

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

它对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.