检查文本字段,文本视图和图像视图以快速激活按钮

问题描述

我正在尝试实施注册视图。 此视图使用图像选择器检查是否设置了图像, 如果所有三个文本字段都不为空,并且下面的文本视图也不为空, 我想实现一个视图,其中下一个按钮被激活。

我已经通过stackoverflow用三个文本字段实现了按钮激活, 但是我不知道如何同时检查图像视图和文本视图。

我已经尝试了将近一个月,但我想不起来。有任何想法吗? 谢谢!

代码

// Properties
@IBOutlet weak var IDTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var checkPasswordTextField: UITextField!
@IBOutlet weak var textView: UITextView!
@IBOutlet weak var nextButton: UIButton!

// ImagePicker
lazy var imagePicker: UIImagePickerController = {
    var picker = UIImagePickerController()
    picker.sourceType = .photoLibrary
    picker.delegate = self
    picker.allowsEditing = true
    return picker
}()

@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    IDTextField.addTarget(self,action: #selector(editingChanged(_:)),for: .editingChanged)
    passwordTextField.addTarget(self,for: .editingChanged)
    checkPasswordTextField.addTarget(self,for: .editingChanged)
    
}

func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediawithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let image: UIImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
        self.imageView.image = image
    }
    self.dismiss(animated: true,completion: nil)
}

// check TextField
@objc func editingChanged(_ textField: UITextField) {
    if textField.text?.count == 1 {
        if textField.text?.first == " " {
            textField.text = ""
            return
        }
    }
    guard
        let ID = IDTextField.text,!ID.isEmpty,let PW = passwordTextField.text,!PW.isEmpty,let CheckPW = checkPasswordTextField.text,!CheckPW.isEmpty,PW == CheckPW
    else {
        nextButton.isEnabled = false
        return
    }
    nextButton.isEnabled = true
}

在这张照片中,我想通过检查图像视图,文本字段和文本视图的状态来激活按钮。但是我不知道如何一次检查所有三个。感谢您的帮助

注册查看图片

sign up view image

解决方法

您要检查的是,是否应针对textFields和imageView中的每次更改启用nextButton。如果您有一个处理检查的功能,则可以在需要检查的任何地方调用它,并保持代码干净。

我将创建一个检查所有必填字段的函数。例如:

/**
 *  This function goes through all fields and checks if they are in the accepted state;
 * - IDTextField should not be empty
 * - passwordTextField and checkPasswordtextField should not be empty and should match
 * - imageView should contain an image
 */
private func toggleNextButton()
{
    if
        !self.IDTextField.isEmpty,let password = self.passwordTextField,password.count > 0,let checkPassword = self.checkPasswordTextField,checkPassword.count > 0,password == checkPassword,self.imageView.image != nil
    {
        self.nextButton.isEnabled = true
    }
    else
    {
        self.nextButton.isEnabled = false
    }
}

对字段中的任何更改调用此函数。您的editingChanged将变为:

// check TextField
@objc func editingChanged(_ textField: UITextField) 
{
    if textField.text?.count == 1 {
        if textField.text?.first == " " {
            textField.text = ""
            return
        }
    }

    self.toggleNextButton()
}

您的imagePickerController将变为:

func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) 
{
    if let image: UIImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
        self.imageView.image = image
    }

    self.toggleNextButton()
}
,

您可以编写一个函数来检查是否需要启用按钮...在textField委托方法中...在textView委托和图像选择器委托中进行检查,以查看是否所有字段都被填充

private func enableTextField()-> Bool {
      if   !IDTextField.text?.isEmpty &&
     !passwordTextField.text?.isEmpty &&
     !checkPasswordTextField?.isEmpty &&
        imageView.image != nil {
         return true
      } else {
        return false
        }
    }