Restrict NSTextField to only allow numbers

前端 未结 9 1618
[愿得一人]
[愿得一人] 2020-12-23 18:31

How do I restrict a NSTextfield to allow only numbers/integers? I\'ve found questions like this one, but they didn\'t help!

9条回答
  •  余生分开走
    2020-12-23 18:56

    In SWIFT, I do it this way

    1. Convert the text value to Int with Int()
    2. Check the converted value is not less than 0
    3. If less than 0, display error message other accept the value

      if ((Int(txtField.stringValue)) < 0){
          // Display error message
      }
      

提交回复
热议问题