用户输入时如何检查 textFiled 输入字符串 [英] How to check the textFiled input string when user inputting
问题描述
我有问题.
我有四个文本字段,但我不知道如何检查输入字符串的文本字段.
I have a problem.
I have four textfields, but I don't know how to check the textfield that input String.
nameField
不超过 10 个字符串ageField
检查是否为数字sexField
检查男性或女性
nameField
doesn't over 10 stringsageField
check whether is number or notsexField
check male or female
这是我的代码:
- (IBAction)addComment:(id)sender {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Commend" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Please input your name:";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Please input your age:";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Please input your sex:";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Please input your commend:";
}];
UIAlertAction *sendAction = [UIAlertAction actionWithTitle:@"send" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
UITextField *nameField = alertController.textFields[0];
UITextField *ageField = alertController.textFields[1];
UITextField *sexField = alertController.textFields[2];
UITextField *commentField = alertController.textFields[3];
UIAlertController *successAlertController = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Success" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self.navigationController popViewControllerAnimated:YES];
}];
[successAlertController addAction:okAction];
[self presentViewController:successAlertController animated:YES completion:nil];
}];
[alertController addAction:sendAction];
[self presentViewController:alertController animated:YES completion:nil];
}
谢谢!
推荐答案
按照以下步骤操作,可能会有所帮助:
Follow below steps, may it help:
将
delegate
和tag
设置为UIAlertController
文本字段.
Set
delegate
andtag
toUIAlertController
textfields.
现在使用 UITextField
委托方法 shouldChangeCharactersInRange
在键入时验证文本或使用 textFieldDidEndEditing
在文本字段结束编辑后进行检查.检查使用它的标签来区分 textFields.
Now use UITextField
delegate method shouldChangeCharactersInRange
to validate text while typing OR use textFieldDidEndEditing
to check after textfield end editing . Check using it's tag to differentiate between textFields.
喜欢
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textField.tag==TxtNameTag){
//Validate Name textField
}
//Same way for other textFields
return YES;
}
OR 使用 textFieldDidEndEditing
在 textField 结束编辑时进行验证.
OR Use textFieldDidEndEditing
to validate while textField end editing.
这篇关于用户输入时如何检查 textFiled 输入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!