如何将键值对附加到 iPhone 上的 UIView?
当我开始 iPhone 开发时,我在某处读到可以将键值对附加到 UIView。 我知道所有 UIView 都可以用作字典来存储您可能想要附加到它们的任何数据,以防止不必要的子类化。 然而,我到处寻找参考,并尝试自己实现该行为,但徒劳无功。
我已经尝试过诸如以下的事情:
UIView *myView = [[UIView alloc] init];
[myView setValue:@"hello" forKey:@"world"];
但这似乎不起作用。 我认为上面的代码所做的是尝试将值@“hello”分配给属性@“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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UIView 与通用键上的键值不兼容。 如果是,那么您的代码示例确实会为键@“world”设置值@“hello”。 但是,层是键值兼容的,因此以下内容可以工作:
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: