【问题标题】:how to get the text field value and call them by the tag value of that text field如何获取文本字段值并通过该文本字段的标记值调用它们
【发布时间】:2016-08-12 06:26:04
【问题描述】:

我有两个文本字段。我没有全局声明。所以它在我的UIAlertController 中。现在textFields 是:

text.tag = 100
text2.tag = 200

所以我有一个选择器方法用于textfield2In 该方法我需要调用textfield1 值是否具有字符串(你好)。这是我的代码:

问题已编辑--

【问题讨论】:

    标签: ios objective-c uialertcontroller


    【解决方案1】:

    喜欢

    选择 1

    您可以访问直接标签

    -(void)textDidChange:(UITextField *)textField
        {
    
    if (textField.text.length > 0) {
    
    
            if (textField.tag == 100) // textField1
            {
    
                    if ([textField.text containsString:@"@"])
                        NSLog(@"Valid");
                     else
                        NSLog(@"Invalid");                    
            }
              else 
             {
               //textField2 work
             }      
    
    }
    
    }
    

    选择 2

    您可以通过对象名称访问

    -(void)textDidChange:(UITextField *)textField
        {
    
    if (textField.text.length > 0) {
    
    
            if (textField == textField1) // 100
            {
    
                    if ([textField.text containsString:@"@"])
                        NSLog(@"Valid");
                     else
                        NSLog(@"Invalid");                    
            }
              else 
             {
               //textField2 work
             }      
    
    }
    
    }
    

    选择 3

    我有两个文本字段。我没有全局声明。 - 为你的概念然后做喜欢

    - (void)textDidChange:(UITextField *)sender
    {
    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
     if (alertController)
     {
    UITextField *txt1 = alertController.textFields.firstObject;
     UITextField *txt2 = alertController.textFields.lastObject;
    
    
    if (textField.text.length > 0) {
    
    
            if (textField == txt1) // 100
            {
    
                    if ([textField.text containsString:@"@"])
                        NSLog(@"Valid");
                     else
                        NSLog(@"Invalid");                    
            }
              else 
             {
               //textField2 work
             }      
    
       }
    }
    

    【讨论】:

    • TextField1 没有添加目标,所以这不起作用
    【解决方案2】:

    请这样做

    -(void)textDidChange:(UITextField *)textField
        {
    
    if(textField.tag == 100){// do stuff here
    
    }else if(textField.tag == 200){
    }
    
    if (textField.text.length > 0) {
    
                    NSString *string = textField.text; // Here i have to call my first textfield value using tag value 
    
                    if ([string containsString:@"@"]) {
                        NSLog(@"Valid");
                    } else {
                        NSLog(@"Invalid");
                    }
    
    
    
    }
    
    }
    

    【讨论】:

    • TextField1 没有添加目标,所以这不起作用
    • @mack 是的,如果它不起作用,请检查并更新我... :)
    【解决方案3】:

    您可以尝试像这样创建一个UITextField 类型的实例变量,并使用您的alertController 文本字段初始化此文本字段。

    UITextField txtField;
    
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField1) {
        textField1.placeholder = @"iCloud ID";
        txtField = textField1;
    }];
    

    现在在你的textField2方法中使用这个textField

    -(void)textDidChange:(UITextField *)textField {
        if (textField.text.length > 0) {
             NSString *string = txtField.text; // Here you will get first textfield value
             if ([string containsString:@"@"]) {
                  NSLog(@"Valid");
             } else {
                  NSLog(@"Invalid");
             }
        }
    }
    

    【讨论】:

    • 你在哪里使用了这个NSString text1
    • 看他想用什么地方
    【解决方案4】:

    你可以使用textFields属性

    警报显示的文本字段数组。 (只读)

    请试一试:

    -(void)textDidChange:(UITextField *)textField
    {
        if (textField.text.length > 0) {
    
            for (UITextField *alertTextField in alertController.textFields) {
    
                if(alertTextField.tag == 100){   // first textfield
    
                    NSString *string = alertTextField.text;
    
                    if ([string containsString:@"@"]) {
                        NSLog(@"Valid");
                    } else {
                        NSLog(@"Invalid");
                    }
                   break; 
                }
            } 
        } 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-02
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 2019-01-18
      • 1970-01-01
      相关资源
      最近更新 更多