我在另一个应用程序中实现了以下代码,但由于某种原因,它在我当前的应用程序中不起作用。即使在文本字段中输入了“禁止”字符,UIAlertView 也不会显示。有谁知道为什么会这样?
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSCharacterSet * set1 = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
if ([textField1.text rangeOfCharacterFromSet:set1].location != NSNotFound) {
UIAlertView *errorAlert1 = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Only numbers are allowed in this field."delegate:self cancelButtonTitle:@"OK."otherButtonTitles:nil];
[errorAlert1 show];
textField1.text=@"";
}
}