I am having a UITextField
in which i get the month number as input. I am successful in limiting the no of characters to 2 in the UITextField. But i want users to enter only the values from 1 to 12
and none other than that. This has to be done simultaneously when the user types the numbers i.e in func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool
. If i use a simple if
condition to check the each character and return false in else
part the textfield won't allow me to use clear or retype any other character. someone help me.
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
see this link-- Limit UITextField input to numbers in Swift
=> you can Define limite of char like this:-
=> and based on define limit char you can use and try it below method :-
You can do it simultaneously by checking the TextField value inside shouldChangeCharactersInRange.
Check out this to set Limit the numbers and allow only numbers 0 to 9.
I just want to post a more simplified answer based on the previous answers.
Tested on Swift 5.1
Considering that you already set
textField.keyboardType = .numberPad
, then you can do the following:You dont´need to validate that
intValue
is greater or equal to0
because innumberPad
you can NOT write negative values.Set keyboard type as Number Pad
add this