Max length UITextField

后端 未结 18 1849
一个人的身影
一个人的身影 2020-11-30 17:42

When I\'ve tried How to you set the maximum number of characters that can be entered into a UITextField using swift?, I saw that if I use all 10 characters, I can\'t erase t

18条回答
  •  心在旅途
    2020-11-30 18:15

    Add More detail from @Martin answer

    // linked your button here
    @IBAction func mobileTFChanged(sender: AnyObject) {
        checkMaxLength(sender as! UITextField, maxLength: 10)
    }
    
    // linked your button here
    @IBAction func citizenTFChanged(sender: AnyObject) {
        checkMaxLength(sender as! UITextField, maxLength: 13)
    }
    
    func checkMaxLength(textField: UITextField!, maxLength: Int) {
        // swift 1.0
        //if (count(textField.text!) > maxLength) {
        //    textField.deleteBackward()
        //}
        // swift 2.0
        if (textField.text!.characters.count > maxLength) {
            textField.deleteBackward()
        }
    }
    

提交回复
热议问题