NSNotification userinfo示例? [英] NSNotification userinfo example?

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

问题描述

我有一个使用CGPoints定位的对象数组。在我的应用程序中的某些时候,数组中的一个对象需要通知其它非数组对象的位置。我知道NSNotification是最好的方式,但我不能找到一个发送者和接收者的通知的包装和展开一个CGPoint作为userinfo一个体面的例子。

I have an array of objects that are positioned using CGPoints . At certain times in my app, an object in the array needs to notify other non-arrayed objects of its position. I understand that NSNotification is the best way to go, but I cant find a decent example of a 'sender' and 'reciever' for the notification that wraps and unwraps a CGPoint as userinfo. Can anyone help?

推荐答案

在Cocoa Touch(但不是Cocoa)中,CGPoints可以用

In Cocoa Touch (but not Cocoa), CGPoints can be wrapped and unwrapped with

+ (NSValue *)valueWithCGPoint:(CGPoint)point
- (CGPoint)CGPointValue

NSValues可以存储在作为userinfo参数传递的NSDictionary中。

NSValues can be stored in the NSDictionary passed as the userinfo parameter.

例如: / p>

For example:

NSValue* value = [NSValue valueWithCGPoint:mypoint];
NSDictionary* dict = [NSDictionary dictionaryWithObject:value forKey:@"mypoint"];

在您的通知中:

NSValue* value = [dict objectForKey:@"mypoint"];
CGPoint newpoint = [value CGPointValue];

这篇关于NSNotification userinfo示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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