如何将当前值设置为在触发控制事件时UITextField
扩展类的类别中声明的属性(通过自定义设置器)?UITextField
editingDidBegin
UITextField
问问题
85 次
1 回答
1
您应该能够通过利用关联引用来使用类别来执行此操作。
使用关联引用,您可以在不修改类声明的情况下向对象添加存储。
这是一个示例,可以让您朝着正确的方向前进:
.h 文件:
@interface UITextField (StoredProperty)
@property (nonatomic, strong) NSString *testString;
@end
.m 文件:
#import <objc/runtime.h>
static void *MyStoredPropertyKey = &MyStoredPropertyKey;
@implementation UITextField (StoredProperty)
- (NSString *)testString {
return objc_getAssociatedObject(self, MyStoredPropertyKey);
}
- (void)setTestString:(NSString *)testString {
objc_setAssociatedObject(self, MyStoredPropertyKey, testString, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
示例使用:
NSObject *obj = [NSObject new];
obj.testString = @"This is my test string";
NSLog(@"%@", obj.testString);
于 2018-08-13T16:48:31.877 回答