我在将textField添加到我的“ shouldChangeCharactersIn”函数中时遇到问题,并且此textField应该为Int。第一个textField应该是String,第二个应该是Int。如果textFields用正确的类型填充,则应该启用Confirm按钮。那是我的应用程序:
和代码:
import UIKit
class LoginViewController: UIViewController, UITextFieldDelegate {
var av = ActualValues()
var statsVC = StatsViewController()
var closureBlock: (() -> Void)?
@IBOutlet weak var idTextField: UITextField!
@IBOutlet weak var idCheckButton: UIButton!
@IBOutlet weak var segmentedControl: UISegmentedControl!
@IBOutlet weak var targetsTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
statsVC.allRunsFunc()
idTextField.delegate = self
idCheckButton.isEnabled = false
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
av.shift = 1
allRunsFunc()
}
@IBAction func idCheckButtonDidPress(_ sender: UIButton) {
av.id = idTextField.text!
av.databaseID = String(runs + 1)
av.target = targetsTextField.text!
closureBlock?()
if let delegate = self.summaryButtonIsEnabledDelegate {
delegate.toggleSummaryButton()
}
self.dismiss(animated: true, completion: nil)
}
@IBAction func segmentedControlChanged(_ sender: UISegmentedControl) {
switch segmentedControl.selectedSegmentIndex {
case 0:
av.shift = 1
case 1:
av.shift = 2
case 2:
av.shift = 3
default:
av.shift = 1
}
}
func configureID(model: ActualValues) {
av = model
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String, int: Int) -> Bool {
let text = (idTextField.text! as NSString).replacingCharacters(in: range, with: string)
if text.isEmpty {
idCheckButton.isEnabled = false
} else {
idCheckButton.isEnabled = true
}
return true
}
}
我应该在该功能中添加什么?
尝试创建函数调用validator()
。您所有的textField应该添加
textField.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)
@objc func textFieldDidChange(_ textField: UITextField) {
// TODO : Validate all of your textField value here
}