【问题标题】:Need to call the same function twice but am getting 'invalid redeclaration' error需要两次调用相同的函数,但出现“无效重新声明”错误
【发布时间】:2016-10-05 08:29:57
【问题描述】:
func textFieldShouldReturn(_ textfield: UITextField) -> Bool{
        p1s1TextField.resignFirstResponder()
        return true
}

func textFieldShouldReturn(_ textfield: UITextField)-> Bool{
        p2s1TextField.resignFirstResponder()
        return true
}

所以这是我需要编写的代码,唯一的区别是它们影响两个不同的文本字段。我了解我需要更改发件人以避免重新声明错误,但不确定将其更改为什么。

【问题讨论】:

  • 您需要读取该方法的参数(textField)。你可以做(​​textField.resignFirstResponder())。如果有不同的用例(if (textField == p1s1TextField){doSomethingSpecifictops1s1}else{}

标签: ios swift xcode redeclaration


【解决方案1】:

只需使用其中一个,parameter textfield 就是您当前的textfield

所以:

func textFieldShouldReturn(_ textfield: UITextField) -> Bool {
        textfield.resignFirstResponder()
        return true
}

【讨论】:

    【解决方案2】:

    尝试根据不同的Text-fields使用委托方法。

    func textFieldShouldReturn(_ textfield: UITextField) -> Bool{
        if textfield == p1s1TextField {
            p1s1TextField.resignFirstResponder()
        }else if textfield == p2s1TextField {
            p2s1TextField.resignFirstResponder()
        }
            return true
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-31
      • 2011-10-31
      • 1970-01-01
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多