检查文本字段、文本视图和图像视图以激活 swift 中的按钮

Check the text field, text view, and image view to activate the button in swift

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

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

试了快一个月了,脑子里想不出来。有任何想法吗? 谢谢!

代码:

// 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, action: #selector(editingChanged(_:)), for: .editingChanged)
    checkPasswordTextField.addTarget(self, action: #selector(editingChanged(_:)), 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
}

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

注册查看图片:

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

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

您想要的是检查是否应为 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()
}