最近我使用 NSKeyValueCoding 协议的函数 func setValue(_ value: AnyObject?, forKey key: String) 来改变UIPickerDate的文本颜色,方法如下: class ColoredDatePicker: UIDatePicker { var changed = false override func addSubvie
NSKeyValueCoding
协议的函数
func setValue(_ value: AnyObject?, forKey key: String)
来改变UIPickerDate的文本颜色,方法如下:
class ColoredDatePicker: UIDatePicker { var changed = false override func addSubview(view: UIView) { if !changed { changed = true self.setValue(UIColor(red: 0.42, green: 0.42, blue: 0.42, alpha: 1), forKey: "textColor") } super.addSubview(view) } }
关于question的答案.它完美无缺.
但我的答案是:
我如何知道该类提供的名称,如上面使用的textColor?
我试图找到任何得到所有名称或在文档中,但到目前为止,我还没有找到任何东西来获得类似上述情况下提供的键.
objective-c运行时为属性提供了这种类型的反射:id UIDatePickerClass = objc_getClass("UIDatePicker"); unsigned int outCount, i; objc_property_t *properties = class_copyPropertyList(UIDatePickerClass, &outCount); for (i = 0; i < outCount; i++) { objc_property_t property = properties[i]; fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property)); }
参考文档:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/index.html#//apple_ref/c/func/class_copyPropertyList
编辑 – 斯威夫特也有基本的反思:https://stackoverflow.com/a/24069875/1385467