如何使用NSNotificationCenter传递对象 [英] How to pass object with NSNotificationCenter

查看:162
本文介绍了如何使用NSNotificationCenter传递对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个对象从我的应用程序委托传递给另一个类中的通知接收者。



我想传递整数 messageTotal 。现在我有:



在收件人:

   - )receiveTestNotification:(NSNotification *)notification 
{
if([[notification name] isEqualToString:@TestNotification])
NSLog(@成功接收测试通知!
}

- (void)viewDidLoad {
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissSheet)name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification :) name:@eRXReceivedobject:nil];

在执行通知的类中:

  [UIApplication sharedApplication] .applicationIconBadgeNumber = messageTotal; 
[[NSNotificationCenter defaultCenter] postNotificationName:@eRXReceivedobject:self];但是我想传递的对象 messageTotal



<到其他类。

解决方案

您必须使用userInfo变体,并传递一个NSDictionary对象,整数:

  NSDictionary * userInfo = @ {@total:@(messageTotal)}; 

NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@eRXReceivedobject:self userInfo:userInfo];

在接收端,您可以访问userInfo字典,如下所示:



<$ c $ p> - (void)receiveTestNotification:(NSNotification *)notification
{
if([notification.name isEqualToString:@TestNotification])
{
NSDictionary * userInfo = notification.userInfo;
NSNumber * total =(NSNumber *)userInfo [@total];
NSLog(@成功接收测试通知!%i,total.intValue);
}
}


I am trying to pass an object from my app delegate to a notification receiver in another class.

I want to pass integer messageTotal. Right now I have:

In Receiver:

- (void) receiveTestNotification:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:@"TestNotification"])
        NSLog (@"Successfully received the test notification!");
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissSheet) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:) name:@"eRXReceived" object:nil];

In the class that is doing the notification:

[UIApplication sharedApplication].applicationIconBadgeNumber = messageTotal;
[[NSNotificationCenter defaultCenter] postNotificationName:@"eRXReceived" object:self];

But I want to pass the object messageTotal to the other class.

解决方案

You'll have to use the "userInfo" variant and pass a NSDictionary object that contains the messageTotal integer:

NSDictionary* userInfo = @{@"total": @(messageTotal)};

NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"eRXReceived" object:self userInfo:userInfo];

On the receiving end you can access the userInfo dictionary as follows:

-(void) receiveTestNotification:(NSNotification*)notification
{
    if ([notification.name isEqualToString:@"TestNotification"])
    {
        NSDictionary* userInfo = notification.userInfo;
        NSNumber* total = (NSNumber*)userInfo[@"total"];
        NSLog (@"Successfully received test notification! %i", total.intValue);
    }
}

这篇关于如何使用NSNotificationCenter传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆