0

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

我已经通过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, 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
}

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

注册查看图片: 注册查看图片

4

2 回答 2

2

你想要的是检查是否nextButton应该为 textFields 和 imageView 中的每个更改启用。如果您有一个处理检查的函数,则可以从需要检查的任何地方调用它并保持代码干净。

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

/**
 *  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()
}
于 2020-08-24T05:50:35.410 回答
1

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

private func enableTextField()-> Bool {
      if   !IDTextField.text?.isEmpty &&
     !passwordTextField.text?.isEmpty &&
     !checkPasswordTextField?.isEmpty &&
        imageView.image != nil {
         return true
      } else {
        return false
        }
    }
于 2020-08-24T05:20:30.723 回答