objeto de acceso pasado en NSNotification?
Tengo una NSNotificación que está publicando un 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]];
¿Cómo me suscribo a esto y obtengo información de este NSDictionary?
En mi viewDidLoad tengo:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recieveInventoryUpdate:) name:@"InventoryUpdate" object:nil];
Y un método en la clase:
- (void)recieveInventoryUpdate:(NSNotification *)notification {
NSLog(@"%@ updated", [notification userInfo]);
}
Que registra un valor nulo, por supuesto.
6 answers
Es [notification object]
También puede enviar userinfo usando notificationWithName:object:userInfo:
método
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-19 14:22:34
Objeto es qué objeto está publicando la notificación, no una forma de almacenar el objeto para que pueda llegar a él. La información del usuario es donde almacena la información que desea conservar con la notificación.
[[NSNotificationCenter defaultCenter] postNotificationName:@"Inventory Update" object:self userInfo:dict];
A continuación, regístrese para la notificación. El objeto puede ser su clase, o nil para recibir todas las notificaciones de este nombre
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recieveInventoryUpdate:) name:@"InventoryUpdate" object:nil];
Luego utilícelo en su selector
- (void)recieveInventoryUpdate:(NSNotification *)notification {
NSLog(@"%@ updated", [notification userInfo]);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-19 14:25:05
Es simple, ver a continuación
- (void)recieveInventoryUpdate:(NSNotification *)notification {
NSLog(@"%@ updated",notification.object); // gives your dictionary
NSLog(@"%@ updated",notification.name); // gives keyname of notification
}
Si accede al notification.userinfo
, devolverá null
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-03 10:06:42
Lo estás haciendo mal. Necesitas usar:
-(id)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)userInfo
Y pasar el dict al último parámetro. El parámetro "objeto" es el objeto que envía la notificación y no el diccionario.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-19 14:23:27
El object
de la notificación está destinado a ser el remitente, en su caso el diccionario no es realmente el remitente, es solo información. Cualquier información auxiliar que se envíe junto con la notificación está destinada a transmitirse junto con el diccionario userInfo
. Enviar la notificación como tal:
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]];
Y luego recibirlo así, para obtener el comportamiento que pretende de una buena manera:
- (void)recieveInventoryUpdate:(NSNotification *)notification {
NSLog(@"%@ updated", [notification userInfo]);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-19 14:30:32
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)
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-11 20:12:52