实现代理方法:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string // return NO to not change text { NSCharacterSet *cs; cs = [[NSCharacterSet characterSetWithCharactersInString:@"123"]invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串 BOOL canChange = [string isEqualToString:filtered]; if (![string isEqualToString:@""]) { if ([filtered isEqualToString:@""]) { //弹出一个提示,提示只能输入123,不能输入以外的字符 } } return canChange; }
时间: 2024-10-12 17:30:53