如何在NSNotification中传递userInfo? [英] How to pass userInfo in NSNotification?

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

问题描述

我试图使用NSNotification发送一些数据,但被卡住。这是我的代码:

I am trying to send some data using NSNotification but get stuck. Here is my code:

// Posting Notification
NSDictionary *orientationData;
if(iFromInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    orientationData = [NSDictionary dictionaryWithObject:@"Right"
                                                  forKey:@"Orientation"];
}

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter postNotificationName:@"Abhinav"
                                  object:nil
                                userInfo:orientationData];

// Adding observer
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(orientationChanged)
                                             name:@"Abhinav"
                                           object:nil];

现在如何在我的选择器 orientationChanged p>

Now how to fetch this userInfo dictionary in my selector orientationChanged?

推荐答案

你得到一个NSNotification对象传递给你的函数。这包括您提供给NSNotificationCenter的名称,对象和用户信息。

You get an NSNotification object passed to your function. This includes the name, object and user info that you provided to the NSNotificationCenter.

- (void)orientationChanged:(NSNotification *)notification
{
    NSDictionary *dict = [notification userInfo];
}

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

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