iPhone 键值绑定
我有一堆存储在 NSArray 中的 NSDictionaries。字典存储按钮的坐标、名称以及按下时要加载的相关对象(UIView)。
当按下按钮时,我可以以字符串格式检索它是哪个按钮,例如“设置”,然后我想运行对象 -(void)settings{}。
现在我知道如何使用 if else 语句来做到这一点,但我想避免它们,因为它们很混乱,并且认为键值绑定会是这样,但我不相信它们存在于 iPhone 上。
它们可以在 iPhone 上使用吗?我读到的所有内容都说“不”,即使在 iOS4 中也是如此。如果他们是我将如何实施这一点。如果没有,除了 if else 语句之外,还有更好的方法吗?我可以存储对 UIView 的引用来推入 NSArray 而不是字符串吗?如果是这样,这会影响性能吗?
干杯。
I have a bunch of NSDictionaries thats stored in an NSArray. The dictionaries store coordinates for buttons, their name, and their relating object to load when pressed (UIView).
When the button is pressed I can retrieve which button it is in string format, for example "settings" and I would like to then run the object -(void)settings{}.
Now I know how I would do it with if else statements but I would like to avoid them because they are messy and thought Key Value Bindings would be the way but I don't believe they exist on the iPhone.
Are they available on iPhone? Everything I read says no even in iOS4. If they are how would I implement this. If not is there a better way to do this other than if else statements? Could I store a reference to the UIView to push in the NSArray instead of a string and if so would this affect performance?
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
iOS 中确实存在键值绑定,但您必须手动进行。您可以创建一个从“Button”派生的类,并且可以创建一个可以分配的 NSDictionary 属性,在分配时,您可以开始观察字典实例中的变化,并且在委托中您可以读取值并将其分配给 self。
好的,这是我的button.h 文件
,这是我的button.m 文件
,可能在代码的开头或您需要执行以下操作的MyButton 实例的任何地方...
Key Value Binding does existing in iOS, but you have to do it manually. You can create a class derived from "Button" and you can create a property of NSDictionary which you can assign and at time of assigning, you can start observing changes in dictionary instance, and in the delegate you can read value and assign to self.
Ok here is my button.h file
and here is my button.m file
and probably in the beginning of code or whereever you have instance of MyButton you need to do following...
如果你想对类和方法进行运行时引用,你需要看看 Objective-C 运行时参考e。这将允许您使用字符串创建类并向它们发送消息。
If you want to do runtime reference of classes and methods you need to have a look at Objective-C Runtime Reference. This will let you by using strings create classes and send messages to them.