How can I declare that a text field can only contain an integer?

前端 未结 9 1890
星月不相逢
星月不相逢 2020-11-30 05:37

In swift, I am trying to make a text field that will allow a button to be enabled, but only when the text field contains an integer. How can I do this?

9条回答
  •  既然无缘
    2020-11-30 06:31

    Each text field has a keyboardType. You could set this to UIKeyboardType.NumbersAndPunctuation to only show numbers and still have the return key present (defensive UI). You could then use NSScanner's scanInt() to check if textField.text is a valid integer.

提交回复
热议问题