我有问题。
我有四个文本字段,但我不知道如何检查输入字符串的文本字段
nameField
不超过10个字符串ageField
检查是否为数字sexField
检查男性或女性这是我的代码:
- (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];
}
谢谢!
答案 0 :(得分:1)
按照以下步骤操作,可能会有所帮助:
将delegate
和tag
设置为UIAlertController
文字字段。
现在使用UITextField
委托方法shouldChangeCharactersInRange
在输入时验证文字或在文本字段结束编辑后使用textFieldDidEndEditing
进行检查。检查使用它的标签来区分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;
}
或使用textFieldDidEndEditing
在textField结束编辑时进行验证。
答案 1 :(得分:0)
尝试使用textField委托方法shouldChangeCharactersInRange
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// here you can get your textfield text and perform an action according to your choice while user is inputting.
return YES; //this make iOS not to perform any action
}
答案 2 :(得分:0)
查看textField delegate
方法shouldChangeCharactersInRange
。每次按textfield
中的任何字符时都会调用它。
答案 3 :(得分:0)
您可以实施UITextFieldDelegate
:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
答案 4 :(得分:0)
使用textfield的标签属性
UITextField *nameField = alertController.textFields[0];
UITextField *ageField = alertController.textFields[1];
UITextField *sexField = alertController.textFields[2];
UITextField *commentField = alertController.textFields[3];
nameField.tag = 1;
ageField.tag = 2;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// identify with tag
if(textField.tag == 1)
{
}
else if (textField.tag == 2)
{
}
return YES;
}
答案 5 :(得分:0)
1.为UITextField设置委托。
2.添加标签。
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Commend" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Please input your name:";
textField.tag=100;
}];
3。将为每个输入的字符调用以下代表。
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField.tag==100){
//Validate Name textField
}
else if(textField.tag==101){
//nextfield textFields
}
return YES;
}
答案 6 :(得分:0)
<your text field>.delegate = self
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
//name text field validation
if textField == nameField{
if textField.text.characters.count < 10 {
return true
}
}
//age text field validation
if textField == ageField{
if Int(textField.text) != nil {
return true
}
}
//sex text field validation
if textField == sexField{
if textField.text + replacementString == "male" {
<he is a man>
}else if textField.text + replacementString == "female"{
<she is a woman>
}
return true
}
return false
}
答案 7 :(得分:0)
[textField addTarget:self action:@selector(textDidChanged :) forControlEvents:UIControlEventEditingChanged];
像这样的副本也可以检查答案 8 :(得分:0)
尝试将JSInputField与modalViewController一起使用
JSInputField *inputField = [[JSInputField alloc] initWithFrame:CGRectMake(10, 100, 300, 50)];
[self.view addSubview:inputField];
[inputField setPlaceholder:@"Please input your name"];
[inputField setRoundedCorners:UIRectCornerAllCorners];
[inputField addValidationRule:JSCreateRuleNotNullValue]; //This will validate field for null value. It will show error if field is empty.
[inputField addValidationRule:JSCreateRuleLength(10)]; //This will validate field for string of length equal to or less than 10.
JSInputField *inputField = [[JSInputField alloc] initWithFrame:CGRectMake(10, 100, 300, 50)];
[self.view addSubview:inputField];
[inputField setPlaceholder:@"Please input your age"];
[inputField setRoundedCorners:UIRectCornerAllCorners];
[inputField addValidationRule:JSCreateRuleNotNullValue]; //This will validate field for null value. It will show error if field is empty.
[inputField addValidationRule:JSCreateRuleNumeric(0)]; //This will validate field for numeric values and restrict to enter value upto 0 decimal places.