I have UITexfields i want that it should accept only number other shows alert that enter a numeric value. I want that motionSicknessTextFiled should only accept number
Swift
class ExampleVC: UIViewController {
let numbers = "0123456789";
override func viewDidLoad() {
super.viewDidLoad()
let textfield = UITextField(frame: CGRectMake(20, 100, 300, 44))
//make some customization, if you want
self.view.addSubview(textfield)
textfield.delegate = self;
}
}
extension ExampleVC: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return string.characters.count > 0 ? numbers.contains(string) : true
}
}