ios 在 NSNotification 中传递的访问对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6748614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:49:29  来源:igfitidea点击:

access object passed in NSNotification?

iosobjective-cnsnotifications

提问by Slee

I have a NSNotification that is posting a NSDictionary:

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

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

我如何订阅这个并从这个 NSDictionary 中获取信息?

in my viewDidLoad I have:

在我的 viewDidLoad 我有:

[[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.

当然,它记录了一个空值。

回答by alex-i

it's [notification object]

它是 [notification object]

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

您还可以使用notificationWithName:object:userInfo:方法发送用户信息

回答by ColdLogic

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

然后注册通知。该对象可以是您的类,或者 nil 仅接收此名称的所有通知

[[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]);
}

回答by Sandy

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.

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

回答by Joris Mans

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.

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

回答by PeyloW

The objectfrom 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 userInfodictionary. Send the notification as such:

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

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

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

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

回答by Juan Boero

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

回答by soorej babu

More simpler way is

更简单的方法是

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

It worked for me.

它对我有用。