Swift - validating UITextField

后端 未结 5 1521
梦谈多话
梦谈多话 2020-11-30 11:58

I have these outlets in my app:

@IBOutlet var name1: UITextField!

@IBOutlet var name2: UITextField!

@IBOutlet var name3: UITextField!

@IBOutlet var name4:         


        
5条回答
  •  猫巷女王i
    2020-11-30 12:38

    The textField:shouldChangeCharactersInRange: method gets called BEFORE the contents of the text field have changed.

    You should special-case the current text field and figure out if it's new contents are going to be blank after the edit.

    Something like this:

    textField.text.length > range.length - replacementString.length
    

    (That's off the top of my head, and not debugged. Plus you would have to come up with logic that makes sure all the other fields are non-blank, and use the logic above to test the contents of the current field.)

提交回复
热议问题