【发布时间】:2016-05-13 10:41:00
【问题描述】:
textField 应该执行以下操作:
User types "w" -> textField: "w?"
User types "ho are you" -> textfield: "who are you?
现在对于每个输入“?”的字符在它之前添加。
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let text = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
let newLength = text.characters.count
if newLength <= 25 {
cLabel.text = String(25 - newLength)
if text.isEmpty { //Checking if the input field is not empty
ahskButton.userInteractionEnabled = false //Disabling the button
ahskButton.enabled = false
} else {
ahskButton.userInteractionEnabled = true //Enabling the button
ahskButton.enabled = true
textField.text! += "?" //NOT WORKING
}
return true;
} else {
return false;
}
}
【问题讨论】:
-
您究竟想要达到什么目标?
-
@luk2302 我希望在用户在文本字段中输入时将问号附加到用户正在输入的字符串的末尾。我也需要禁用一个按钮,只要 textField 为空并且需要限制和显示字符数。
标签: ios swift swift2 uitextfield