ios - How to make 3 textFields in a ViewController take only integer input - XCode -
i have 3 textfields called text1,text2, , text3.
how program them accept integer input?
most of similiar questions i've seen don't work in newest version of xcode (using swift 2) , don't cater multiple textfields.
this 1 seemed helpful: how can declare text field can contain integer?
first change keyboardtype
uikeyboardtypenumberpad
suggested ianurag
and check content change in delegate method shouldchangecharactersinrange
(code tested swift 2.0 , xcode 7)
func textfield(textfield: uitextfield, shouldchangecharactersinrange range: nsrange, replacementstring string: string) -> bool { // find out text field after adding current edit let text = (textfield.text! nsstring).stringbyreplacingcharactersinrange(range, withstring: string) if text == "" { return true } if let _ = int(text) { return true } else { return false } }
Comments
Post a Comment