自定义NSFormatter和错误消息
问题描述:
我需要为某些字段创建一个自定义的NSFormatter,但如果无效值,我不想使用警报表来显示错误消息...我更喜欢只使用NSBeep( )。这是可能的,或者我必须提出由NSFormatter提出的标准工作表的错误?自定义NSFormatter和错误消息
答
在你NSFormatter
子类实现,你应该能够做这样的事情:
@implementation MyFormatter
- (BOOL)isPartialStringValid:(NSString *)partialString
newEditingString:(NSString **)newString
errorDescription:(NSString **)error
{
// Test if the string is too long, for example 5 characters
if ([partialString length] > 5)
{
NSBeep();
return NO;
}
// other tests here
*newString = partialString;
return YES;
}
@end