如何在iPhone上查看键值对到UIView? [英] How do I attatch a Key value pair to a UIView on iPhone?

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

问题描述

当我开始iPhone开发时,我读到某个地方,可以附加一个键值对到UIView。我理解所有的UIViews可以用作字典来存储任何数据,你可能想要对他们进行防止不必要的子类化。



我尝试过以下的操作:

  UIView * myView = [[UIView alloc] init]; 
[myView setValue:@helloforKey:@world];

但这似乎不起作用。我认为上面的代码是尝试分配值@hello到属性@世界 - 这不是我想要的。



是什么我想尽可能实现?



任何帮助都将非常感激。



谢谢! p>

Nick。

解决方案

UIViews不符合一般键的键值。如果他们是,那么你的代码示例将确实为键@world设置值@hello。

  UIView * myView = 

[[UIView alloc] init];
[myView.layer setValue:@helloforKey:@world];


When I started iPhone development, I read somewhere that it's possible to attach a key value pair to a UIView. I understood that all UIViews could be used as dictionaries to store any data you may want to attatch to them to prevent unnecessary subclassing. However, I've searched everywhere to find the reference and tried to implement the behavior myself in vain.

I've tried things such as:

UIView *myView = [[UIView alloc] init];
[myView setValue:@"hello" forKey:@"world"];

but this doesn't seem to work. I think what the above code does is tried to assign the value @"hello" to the property @"world" - which isn't what I want.

Is what I'm trying to achieve possible?

Any help here would be much appreciated.

Thanks!

Nick.

解决方案

UIViews are not key-value compliant on generic keys. If they were, then your code sample would indeed set the value @"hello" for the key @"world". However, layers are key-value compliant, so the following would work:

UIView *myView = [[UIView alloc] init];
[myView.layer setValue: @"hello" forKey: @"world"];

这篇关于如何在iPhone上查看键值对到UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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