代码之家  ›  专栏  ›  技术社区  ›  alecnash

一个视图上有多个UITextFields

  •  2
  • alecnash  · 技术社区  · 15 年前

    我对uitextfield有点问题。我在一个视图中使用其中的两个,当我向第二个字段写入内容时,第一个字符串也会被更改。

    -(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    textString = textField.text;
    NSLog(@"the string1 %@",textString);
    [textField resignFirstResponder];
    
    
    textString2 = textField2.text;
    NSLog(@"the string2 %@",textString2);
    [textField2 resignFirstResponder];
    
    return YES;}
    

    所以我需要帮助。

    2 回复  |  直到 15 年前
        1
  •  11
  •   Swapnil Luktuke    15 年前

    你的 textFieldShouldReturn 方法,因此需要区分每个字段的操作。

    为两个文本字段设置一个标记:

    myTextField1.tag = 100;
    myTextField2.tag = 101;
    

    并检查标签在 方法:

    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        if(textField.tag == 100)
        {
            textString = textField.text;
        }
        else if(textField.tag == 101)
        {
            textString2 = textField.text;
        }
    
        [textField resignFirstResponder];
    }
    

    这里textField是传递给委托的对象,即您点击return的对象。所以用它而不是你的对象。

    请避免将文本字段命名为textField和textField2这是一个非常糟糕的编码实践。

    祝你好运

        2
  •  0
  •   Ross Light    15 年前

    听起来像你的 IBOutlets