我有三个不同的文本框,在其中需要使用3个不同的“ textViewDidChange”方法。将它们组合为一个功能对我不起作用,因为它们是三种可能相互影响的不同动作。所以我试图做以下三个不同的功能。但是我收到“无效的重新声明”错误。我该如何解决?

func textViewDidChange(messageTxt1: UITextView) {

    var len = count(messageTxt.text.utf16)

    var diff = 90 - len

    if diff < 0 {

        charsLbl.textColor = UIColor.redColor()
    } else {

        charsLbl.textColor = UIColor.blackColor()
    }

    charsLbl.text = "\(diff) chars left"

}


func textViewDidChange (messageTxt2: UITextView) {

    refreshResults()

    var len2 = count(messageTxt2.text.utf16)

    if len2 > 3 {

        println("search")

    }

    else

    {

        resultsTable.hidden = true

    }

    var diff2 = 30 - len2

    if diff2 < 0 {

        charsLbl2.textColor = UIColor.redColor()
    } else {

        charsLbl2.textColor = UIColor.blackColor()
    }

    charsLbl2.text = "\(diff2) chars left"


}


func textViewDidChange (messageTxt3: UITextView) {


    var len3 = count(messageTxt3.text.utf16)

    var diff3 = 30 - len3

    if diff3 < 0 {

        charsLbl3.textColor = UIColor.redColor()
    } else {

        charsLbl3.textColor = UIColor.blackColor()
    }

    charsLbl3.text = "\(diff3) chars left"


}

最佳答案

您正在创建具有相同名称textViewDidChange的三个方法。
在您的情况下,请使用tag属性,并为全部定义一个方法。




let textView1 = UITextView()
textView1.tag = 1

let textView2 = UITextView()
textView2.tag = 2

let textView3 = UITextView()
textView3.tag = 3

In method just add condition for that.

func textViewDidChange (messageTxt: UITextView) {

if messageTxt.tag == 1
{
     // Your code for textview 1
}
else if messageTxt.tag == 2
{
      // Your code for textview 2
}
else if messageTxt.tag == 3
{
    // Your code for textview 3
}}

10-07 14:28