ios - 仅当所有文本字段均已验证成功时才启用登录按钮

标签 ios swift

我想在满足文本字段的所有条件后启用登录按钮。我已通过右舷向两个文本字段添加了 editchange 事件。默认情况下,我向具有红色背景颜色的文本字段添加了一个标签,以指示(错误)一旦用户在文本字段中输入正确的值,那么我需要更改标签颜色为绿色。它不能正常工作。

First

Second

我的整个代码:

import UIKit
import IQKeyboardManagerSwift

class SignInFormViewController: UIViewController , UITextFieldDelegate {

    @IBOutlet weak var forgotPasswordButton: UIButton!
    @IBOutlet weak var signInButton: UIButton!

    // Existing User
    @IBOutlet weak var existingEmailAddressTextField: UITextField!
    @IBOutlet weak var existingPasswordTextField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
            //Setting text fileds effect
        self.makeTextFiledEffect(textField: existingEmailAddressTextField, backColor: UIColor.red, borderColor: UIColor.lightGray)
        self.makeTextFiledEffect(textField: existingPasswordTextField, backColor: UIColor.red, borderColor: UIColor.lightGray)

        //Disable sing button by default
        signInButton.isEnabled = false
    }

    func makeTextFiledEffect(textField : UITextField, backColor : UIColor, borderColor : UIColor) {
        for viewPrevious in textField.subviews{
            if viewPrevious.tag == 1000{
                viewPrevious.removeFromSuperview()
            }
        }
        let arrowView = UILabel(frame: CGRect(x:0, y: 0, width: 5, height: textField.frame.size.height))
        arrowView.tag = 1000
        arrowView.backgroundColor = backColor
        textField.changeDynamicBorderColor(borderColor: borderColor)
        textField.addSubview(arrowView)
    }

    @IBAction func validateTextField(_ sender: UITextField) {
        if sender.text?.count == 1 {
            if sender.text?.first == " " {
                sender.text = ""
                return
            }
        }
        guard
            let emaiTextField = existingEmailAddressTextField.text, !emaiTextField.isEmpty && self.validateEmail(emaiTextField)==true ,
            let password = existingPasswordTextField.text, !password.isEmpty
            else {
                self.makeTextFiledEffect(textField: sender, backColor: UIColor.red, borderColor: UIColor.lightGray)
                self.signInButton.isEnabled = false
                return
        }
        self.makeTextFiledEffect(textField: sender, backColor: UIColor.green, borderColor: UIColor.lightGray)
        signInButton.isEnabled = true

    }
    @IBAction func signInButtonTapped(_ sender: UIButton) {
        print("Tapped")

    }



func validateEmail(_ candidate: String) -> Bool {

        let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
        return NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluate(with: candidate)
    }
}

extension UITextField
{
    func changeDynamicBorderColor(borderColor : UIColor){
        self.layer.borderColor = borderColor.cgColor
    }
    open override func draw(_ rect: CGRect) {
        self.layer.cornerRadius = 10.0
        self.layer.borderWidth = 1.0
//        self.layer.borderColor = UIColor.lightGray.cgColor
        self.layer.masksToBounds = true
    }
}

您可以找到类似的问题:Enable a button in Swift only if all text fields have been filled out

提前致谢..

最佳答案

请按照以下基本步骤来实现您的目标:-

  • 创建一个函数来执行所有验证(UI 级别或 API 级别或任何验证)。如果任何一个验证失败,则返回 false,否则返回 true
  • loginButton.isEnabled = 上述函数的返回值

关于ios - 仅当所有文本字段均已验证成功时才启用登录按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49318030/

相关文章:

ios - HKHealthStore 类型的值没有类型 dateOfBirthWithError

ios - 谷歌分析时间未显示

ios - 防止通话状态栏影响 View

ios - UICollectionView visibleCells 在滚动前返回 0

ios - Swift 3 到 4 - '#selector' 的参数引用未公开给 Objective-C 的实例方法 'handleScreenTap(sender:)'

ios - 为什么不能在 Swift 中为 @IBOutlet 分配 let 而不是 var?

ios - 快速转换: ERROR - CGPathAddArc

swift - Rx swift : What is the best practice to use DisposeBag?

ios - 在 Swift 的 UIView 中查找 UILabel

objective-c - 在 iOS 中传递参数