So my question is about making a text filter in Swift for iOS. The idea is about making an alert such that if the user has inputed a string it would alert the user to input only numbers, because the program is a calculator. So where in this filter do I need to put the alert code?
class ViewController: NSObject, UITextFieldDelegate {
func TextField(TextField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
var result = true
if countElements(string) > 0 {
let numericInput = NSCharacterSet(charactersInString: "0123456789.-").invertedSet
if let badRange = string.rangeOfCharacterFromSet(numericInput) {
let substring = string.substringToIndex(badRange.startIndex)
let oldString: NSString = TextField.text // necessary so we can use the NSRange object passed in.
TextField.text = oldString.stringByReplacingCharactersInRange(range, withString: substring)
result = false
}
}
return false
}
}