【问题标题】:Change the text of a UITextField on text change of another text field在另一个文本字段的文本更改时更改 UITextField 的文本
【发布时间】:2016-04-19 09:19:25
【问题描述】:

我有六个文本字段。用户无法编辑这些字段之一。相反,它从其他五个中获取输入,将其压缩为一个字符串(每个输入之间有冒号,并有一定的排列)。

任何时候修改任何启用的文本字段,第六个文本字段的内容都会改变,输入的顺序不会改变。因此,如果我在前五个文本字段中输入1,2,3,4,5,第六个将显示为1:2:3:4:5。如果我将第二个文本字段的文本从 2 更改为 two,第六个文本字段的文本也会更改为 1:two:3:4:5

我知道如何做到这一点,创建五个全局变量,保存五个文本字段的值,然后从其中的一个 concat 中读取第六个。我不知道如何触发文本更改。

我试过用这个:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

}

以及其他委托方法,但我不确定这是要走的路。

【问题讨论】:

  • 如果用户没有在第三个文本字段中输入值怎么办? “1:two:4:5”?
  • @the_UB,是的。跳过那个,并跳过它后面的冒号。

标签: ios cocoa-touch uitextfield


【解决方案1】:

创建一个数组:

var myArray= [String]()

在 viewDidLoad 中执行此操作,当用户对文本字段进行一些更改时分配操作(对所有 textfield/1、2、3、4、5 执行此操作):

textField1.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)

textField2.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)

当用户编辑某些内容时,此方法将被调用:

func textFieldDidChange(textField: UITextField) {
    myArray.removeAll()

    if !textField1.isEmpty {
        myArray.append(textField1.text)
    }

    if !textField2.isEmpty {
        myArray.append(textField2.text)
    }

    if !textField3.isEmpty {
        myArray.append(textField3.text)
    }

    if !textField4.isEmpty {
        myArray.append(textField4.text)
    }

    if !textField5.isEmpty {
        myArray.append(textField5.text)
    }

    textField6.text = myArray.joinWithSeparator(":")
}

【讨论】:

  • 谢谢。值得注意的是,":".join(myArray) 不适用于 Swift 2。必须改用 myArray.joinWithSeparator(":")
【解决方案2】:

您可以使用其名称访问特定的文本字段。例如 ;

func textFieldDidEndEditing(textField: UITextField) {

    sixthTextField.text = "\(firstTextField.text):\(secondTextField.text)\(thirdTextField.text):\(fourthTextField.text)\(fifthTextField.text)"

}

该方法会在 textField 结束编辑时执行代码。如果你想检查它是否有特定的 textField ;

func textFieldDidEndEditing(textField: UITextField) {
    if (textField == secondTextfield) {
       sixthTextField.text = "\(firstTextField.text):\(secondTextField.text)\(thirdTextField.text):\(fourthTextField.text)\(fifthTextField.text)"
   }
}

只有在 secondTextField 结束编辑时才会执行代码。

不要忘记将 UITextFieldDelegate 添加到您的类中,并且不要忘记在 viewDidLoad 中设置 textField 的委托。

class MyClass: UIViewController, UITextFieldDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        firstTextField.delegate = self
        secondTextField.delegate = self
        thirdTextField.delegate = self
        fourthTextField.delegate = self
        fifthTextField.delegate = self
        sixthTextField.delegate = self

    }
}

或者您可以为 valueChanged 事件的每个 textField 创建 IBAction。与 UIButton IBAction 相同,唯一的区别是事件。您应该选择 valueChanged 作为事件。

@IBAction func firstTextFieldValueChanged(sender: UITextField) {

         sixthTextField.text = "\(firstTextField.text):\(secondTextField.text):\(thirdTextField.text):\(fourthTextField.text):\(fifthTextField.text)"
}

@IBAction func secondTextFieldValueChanged(sender: UITextField) {

         sixthTextField.text = "\(firstTextField.text):\(secondTextField.text):\(thirdTextField.text):\(fourthTextField.text):\(fifthTextField.text)"
}

@IBAction func thirdTextFieldValueChanged(sender: UITextField) {

         sixthTextField.text = "\(firstTextField.text):\(secondTextField.text):\(thirdTextField.text):\(fourthTextField.text):\(fifthTextField.text)"
}

@IBAction func fourthTextFieldValueChanged(sender: UITextField) {

         sixthTextField.text = "\(firstTextField.text):\(secondTextField.text):\(thirdTextField.text):\(fourthTextField.text):\(fifthTextField.text)"
}

@IBAction func fifthTextFieldValueChanged(sender: UITextField) {

         sixthTextField.text = "\(firstTextField.text):\(secondTextField.text):\(thirdTextField.text):\(fourthTextField.text):\(fifthTextField.text)"
}

【讨论】:

    【解决方案3】:

    您可以实现didEndEditing 委托并在那里执行您的逻辑。

    func textFieldDidEndEditing(textField: UITextField) {
        var text1:String?
        var text2:String?
        var text3:String?
        var text4:String?
        var text5:String?
    
        if ((textField1.text) != nil) {
            text1 = textField1.text
        } else {
            text1 = ""
        }
    
        if ((textField2.text) != nil) {
            text2 = textField2.text
        } else {
            text2 = ""
        }
    
        if ((textField3.text) != nil) {
            text3 = textField3.text
        } else {
            text3 = ""
        }
    
        if ((textField4.text) != nil) {
            text4 = textField4.text
        } else {
            text4 = ""
        }
    
        if ((textField5.text) != nil) {
            text5 = textField5.text
        } else {
            text5 = ""
        }
    
        textField6.text = String(format: "%@:%@:%@:%@:%@", text1, a2, a3, a4, a5)
    }
    

    添加文本字段委托并将其实现到 textfield1 - textfield5 到

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 2017-10-15
      • 1970-01-01
      • 2011-10-24
      • 1970-01-01
      相关资源
      最近更新 更多