No dudes en compartir nuestro espacio y códigos en tus redes, danos de tu ayuda para ampliar esta comunidad.
Solución:
Puedes usar UITextFieldDelegate
‘s shouldChangeCharactersInRange
método para limitar la entrada del usuario a números:
func textField(textField: UITextField,
shouldChangeCharactersInRange range: NSRange,
replacementString string: String) -> Bool
// Create an `NSCharacterSet` set which includes everything *but* the digits
let inverseSet = NSCharacterSet(charactersInString:"0123456789").invertedSet
// At every character in this "inverseSet" contained in the string,
// split the string up into components which exclude the characters
// in this inverse set
let components = string.componentsSeparatedByCharactersInSet(inverseSet)
// Rejoin these components
let filtered = components.joinWithSeparator("") // use join("", components) if you are using Swift 1.2
// If the original string is equal to the filtered string, i.e. if no
// inverse characters were present to be eliminated, the input is valid
// and the statement returns true; else it returns false
return string == filtered
Actualizado para Swift 3:
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool
// Create an `NSCharacterSet` set which includes everything *but* the digits
let inverseSet = NSCharacterSet(charactersIn:"0123456789").inverted
// At every character in this "inverseSet" contained in the string,
// split the string up into components which exclude the characters
// in this inverse set
let components = string.components(separatedBy: inverseSet)
// Rejoin these components
let filtered = components.joined(separator: "") // use join("", components) if you are using Swift 1.2
// If the original string is equal to the filtered string, i.e. if no
// inverse characters were present to be eliminated, the input is valid
// and the statement returns true; else it returns false
return string == filtered
Para cualquiera que busque una respuesta más corta, he encontrado esto bastante útil.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
// remove non-numerics and compare with original string
return string == string.filter("0123456789".contains)
Funciona en XCode 10.1, Swift 4.2
Si entiendes que te ha resultado de ayuda nuestro artículo, nos gustaría que lo compartas con el resto desarrolladores de esta forma nos ayudas a dar difusión a este contenido.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)