我有TextField,其值绑定到Document的serverAddress属性(readwrite,copy),TextField formatter
委托与 XIB 中的ServerAddressFormatter
对象相关联。< / p>
它实际上正在处理像127.0.0.1:8080
这样的输入,但是只要我放了一些没有:
TextField的内容就完全清楚了。
此处ServerAddressFormatter
实施:
@implementation ServerAddressFormatter
- (NSString *) stringForObjectValue:(NSArray *)obj {
if ([obj isKindOfClass:[NSArray class]]) {
return [obj componentsJoinedByString:@":"];
} else {
return @"";
}
}
- (BOOL)getObjectValue:(out id *)anObject
forString:(NSString *)string
errorDescription:(out NSString **)error {
int i;
for (i = (int) ([string length] == 0 ? 0 : [string length] - 1); i > 0; i--) {
if ([string characterAtIndex:i] == ':') break;
}
if (i == 0) {
*anObject = @[string]; // if I put string, @"100" here it's working fine
} else {
*anObject = @[[string substringToIndex:i], [string substringFromIndex:i+1]];
}
return YES;
}
@end