在NSNotification中传递的访问对象?

时间:2011-07-19 14:16:05

标签: ios objective-c nsnotifications

我有一个发布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]);
}

当然记录一个空值。

7 个答案:

答案 0 :(得分:34)

它是[notification object]

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

发送userinfo

答案 1 :(得分: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]);
}

答案 2 :(得分:3)

这很简单,见下文

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

}

如果访问notification.userinfo,则会返回null

答案 3 :(得分:2)

你做错了。你需要使用:

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

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

答案 4 :(得分: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]);
}

答案 5 :(得分: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)
}

答案 6 :(得分:0)

更简单的方法是

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

对我有用。