所以我的问题是在Swift for iOS中制作文本过滤器。该想法是关于发出警报,使得如果用户输入了字符串,则它将警告用户仅输入数字,因为该程序是计算器。那么在这个过滤器中我需要放置警报代码?
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
}
}
答案 0 :(得分:-2)