【问题标题】:How to check the textFiled input string when user inputting用户输入时如何检查文本字段输入字符串
【发布时间】:2016-08-31 10:22:30
【问题描述】:

我有问题。
我有四个文本字段,但我不知道如何检查输入字符串的文本字段。

  • 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];

}

谢谢!

【问题讨论】:

  • 您想在输入时检查是否输入了正确的数据?还是按下发送按钮后?
  • 你如何检查它的男性或女性
  • 为什么你不使用年龄和性别字段的选择器????
  • 我想在输入结束时检查并转到下一个文本字段

标签: ios objective-c iphone uitextfield


【解决方案1】:

请按照以下步骤操作,可能会有所帮助:

  1. delegatetag 设置为UIAlertController 文本字段。

  2. 现在使用 UITextField 委托方法 shouldChangeCharactersInRange 在键入时验证文本或使用 textFieldDidEndEditing 在文本字段结束编辑后检查。检查使用它的标签来区分文本字段。

喜欢,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

if(textField.tag==TxtNameTag){

   //Validate Name textField
}
//Same way for other textFields


    return YES; 
}

或者在 textField 结束编辑时使用 textFieldDidEndEditing 进行验证。

【讨论】:

  • 谢谢!帮我很多。
  • 太棒了:) @imbeginner_sorry
【解决方案2】:

试试 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
}

【讨论】:

    【解决方案3】:

    查看textField delegate 方法shouldChangeCharactersInRange。每次按下textfield中的任何字符都会调用它。

    【讨论】:

      【解决方案4】:

      你可以实现UITextFieldDelegate:

      - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
      

      Doc

      【讨论】:

        【解决方案5】:

        请使用文本字段的标签属性

        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; 
            }
        

        【讨论】:

          【解决方案6】:

          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; 
           }
          

          【讨论】:

            【解决方案7】:
            <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
            }
            

            【讨论】:

              【解决方案8】:

              [textField addTarget:self action:@selector(textDidChanged:) forControlEvents:UIControlEventEditingChanged];

              喜欢这个副本也可以查看

              【讨论】:

                【解决方案9】:

                尝试将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.
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-02-25
                  • 2022-06-17
                  • 1970-01-01
                  • 2011-06-26
                  相关资源
                  最近更新 更多