有一个UITextField,要求用户不能输入小于1大于100的数字(不是字符数,而是数字)
有一种方法——shouldChangeCharactersIn range,但不能同时限制 MAX 和 MIN:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
var maxValue = 100 {
didSet {
textField?.text = nil
}
}
}
extension ViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newText = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
if newText.isEmpty {
return true
}
else if let intValue = Int(newText), intValue <= maxValue {
return true
}
return false
}
}
protocol AsyncGeneratorType {
associatedtype Element
associatedtype Fetch
func next(_ fetchNextBatch: Fetch)
}
您不能输入 0 或负数。和超过 100 的数字。
这就是你的方法 - 检查范围内的条目