swift 如何检查UITextField是否再次变空?

x6h2sr28  于 2022-10-31  发布在  Swift
关注(0)|答案(3)|浏览(232)

我正在用VC创建一个简单的日志。让我们忽略用户名和密码的输入验证。我只想在用户名和密码的UITextField都不为空时启用UIButton。并且每当其中任何一个为空时,我想禁用该按钮。

@IBAction func typingUserName(_ sender: Any) {
    usernameTxtfield.layer.shadowPath = UIBezierPath(rect: usernameTxtfield.bounds).cgPath
    usernameTxtfield.layer.shadowRadius = 2
    usernameTxtfield.layer.shadowOffset = .zero
    usernameTxtfield.layer.shadowOpacity = 0.5

    signInIcon.isEnabled = false
}

@IBAction func typingPassword(_ sender: Any) {
    passwordTxtfield.layer.shadowPath = UIBezierPath(rect: passwordTxtfield.bounds).cgPath
    passwordTxtfield.layer.shadowRadius = 2
    passwordTxtfield.layer.shadowOffset = .zero
    passwordTxtfield.layer.shadowOpacity = 0.5

    signInIcon.isEnabled = false
}

@IBAction func usernameTxtFieldEditingChnged(_ sender: Any) {
    usernameTxtfield.layer.shadowRadius = 0
    usernameTxtfield.layer.shadowOffset = .zero
    usernameTxtfield.layer.shadowOpacity = 0

}

@IBAction func passwordEditingChaned(_ sender: Any) {
    passwordTxtfield.layer.shadowRadius = 0
    passwordTxtfield.layer.shadowOffset = .zero
    passwordTxtfield.layer.shadowOpacity = 0

    signInIcon.isEnabled = true
}

@IBAction func signInClicked(_ sender: Any) {
    performSegue(withIdentifier: "welcomeVC", sender: signInIcon)
}

正如您所看到的,我只在密码文本字段EditingChanged被触发后才启用该按钮。

uqcuzwp8

uqcuzwp81#

您可以观察事件.editingChanged

passwordTxtfield.addTarget(self, action: #selector(passwordEditingChaned), for: .editingChanged)
usernameTxtfield.addTarget(self, action: #selector(usernameTxtFieldEditingChnged), for: .editingChanged)

然后在两种方法中添加检查:

signInIcon.isEnabled = passwordTxtfield.text?.isEmpty == false && usernameTxtfield.text?.isEmpty == false
hc2pp10m

hc2pp10m2#

您必须实现如下内容:

if usernameTxtfield.text != "" && passwordTxtfield.text != "" {
    signInIcon.isEnabled = true
} else {
    signInIcon.isEnabled = false
}

您可以在每个UITextField的操作中添加这段代码,然后就可以开始了

anhgbhbe

anhgbhbe3#

将文本字段按钮出口连接到视图控制器类。

@IBOutlet private weak var usernameTxtfield: UITextField!
@IBOutlet private weak var passwordTxtfield: UITextField!

@IBOutlet private weak var signInButton: UIButton!

然后编写设置函数

private func setupTextFields() {
    // write your TextFields custom setup ...
    // also add delegate lines

    usernameTxtfield.delegate = self
    passwordTxtfield.delegate = self
}

将setupTextFields函数设置为VC的viewDidLoad,并将按钮设置为isEnable = false

override func viewDidLoad() {
    super.viewDidLoad()

    setupTextFileds()

    setSignInButton(isEnable: false)
}

并编写func来获取textFields。

private func getTextFields() -> [UITextField] {
    return [usernameTxtfield, passwordTxtfield]
}

我们还需要检查这些是否有效。

private func isInputsValid() -> Bool {
    var isValid: Bool = true

    let inputs: [UITextField] = getTextFields()

    if let input = inputs.first(where: { $0.text?.count == 0 }) {
        debugPrint("\(input) is not valid.")
        isValid = false
    }

    return isValid
}

也将函数添加到set Button

private func setSignInButton(isEnable: Bool) {
    signInButton.isEnabled = isEnable
}

然后编写TextFielddelegatefunc以了解输入正在更改并更改按钮的条件。

extension ViewController: UITextFieldDelegate {

    func textFieldDidChangeSelection(_ textField: UITextField) {
        setSignInButton(isEnable: isInputsValid())
    }
}

我希望这对你有帮助:)

相关问题