在UITextView&中输入内容后未启用UIBarButtonItem的UITextField

时间:2018-05-28 13:11:59

标签: swift uitextfield uitextview uibarbuttonitem

我有一个UIViewController,其中包含3个UITextFields和一个UITextView。我需要的强制性内容是UITextFields名为postTitleTextField或者只有UITextView才能拥有内容,然后才能启用类型为UIBarButtonItem的postButton。我做错了什么,并没有像我期望的那样工作。

import UIKit

class PostTextVC: UIViewController {

    @IBOutlet weak var postButton: UIBarButtonItem!
    @IBOutlet weak var postTitleTextField: UITextField!
    @IBOutlet weak var postContentTextView: UITextView!
    @IBOutlet weak var postTagsTextField: UITextField!
    @IBOutlet weak var postSourceTextField: UITextField!
    var textViewPlaceholderLabel : UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.navigationBar.barTintColor = PostMenuColors.yellow2
        navigationController?.navigationBar.tintColor = .white
        setupTextFields()
        setupContentTextView()
        textFielsdDelegate()
    }

    @IBAction func cancelButtonPressed(_ sender: UIBarButtonItem) {
        dismiss(animated: true, completion: nil)
    }

    @IBAction func optionsButtonPressed(_ sender: UIBarButtonItem) {

    }

    @IBAction func postButtonPressed(_ sender: UIBarButtonItem) {
        print("Post pressed")
    }
}

extension PostTextVC: UITextFieldDelegate
{
    func setupTextFields()
    {
        setupTextFieldUI(textField: postTitleTextField)
        setupTextFieldUI(textField: postSourceTextField)
        setupTextFieldUI(textField: postTagsTextField)
        postButton.isEnabled = false
       // handleTextFields()
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }

    // Note: Listens for changes for ALL text frields
    func handleTextFields()
    {
        postTitleTextField.addTarget(self, action: #selector(self.textFieldDidChange), for: .editingChanged)
    }

    func textFielsdDelegate()
    {
        postTitleTextField.delegate = self
        postTagsTextField.delegate = self
        postSourceTextField.delegate = self
    }

    // Note: Checks for value in all text fields and/or disable/enable the sign in button
    @objc func textFieldDidChange()
    {
        guard let postTitle = postTitleTextField.text, !postTitle.isEmpty
            else
        {
            self.postButton.tintColor = UIColor.lightGray
            self.postButton.isEnabled = false
            return
        }

        guard let postContent = postContentTextView.text, !postContent.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).isEmpty
            else
        {
            self.postButton.tintColor = UIColor.lightGray
            self.postButton.isEnabled = false
            return
        }
        postButton.isEnabled = true
        postButton.tintColor = Colors.mainBlueColor
    }

    func setupTextFieldUI(textField: UITextField)
    {
        textField.tintColor = .darkGray
        textField.textColor = .darkGray
        textField.borderStyle = .none
        let bottomLine = CALayer()
        bottomLine.frame = CGRect(x: 0, y: 29, width: textField.bounds.width, height: 1)
        bottomLine.backgroundColor = UIColor.gray.cgColor
        textField.layer.addSublayer(bottomLine)
        textField.backgroundColor = .clear
        textField.attributedPlaceholder = NSAttributedString(string : textField.placeholder!,
                                                          attributes: [NSAttributedStringKey.foregroundColor: Colors.authTextFieldPlaceholderColor])
    }
}

// MARK: Text View
extension PostTextVC : UITextViewDelegate {

    func setupContentTextView()
    {
        postContentTextView.delegate = self
        textViewPlaceholderLabel = UILabel()
        textViewPlaceholderLabel.text = "Enter your thoughts"
        textViewPlaceholderLabel.font = UIFont.init(name: Fonts.OpenSans_Regular, size: (postContentTextView.font?.pointSize)!)
        textViewPlaceholderLabel.sizeToFit()
        postContentTextView.addSubview(textViewPlaceholderLabel)
        textViewPlaceholderLabel.frame.origin = CGPoint(x: 0, y: (postContentTextView.font?.pointSize)! / 2)
        textViewPlaceholderLabel.textColor = UIColor.lightGray
        textViewPlaceholderLabel.isHidden = !postContentTextView.text.isEmpty

    }

    func textViewDidChange(_ textView: UITextView) {
        textViewPlaceholderLabel.isHidden = !postContentTextView.text.isEmpty
    }
}

1 个答案:

答案 0 :(得分:0)

我自己想通了。基本上,我不得不改变两件事:

  1. func textFieldDidChange()

    @objc func textFieldDidChange()
    {
    guard let postTitle = postTitleTextField.text, !postTitle.isEmpty
        else
    {
        self.postButton.tintColor = UIColor.lightGray
        self.postButton.isEnabled = false
        return
    }
    postButton.isEnabled = true
    postButton.tintColor = Colors.mainBlueColor
    }
    
  2. textViewDidChange()

    func textViewDidChange(_ textView: UITextView) {
    textViewPlaceholderLabel.isHidden = !postContentTextView.text.isEmpty
    
    guard let postContent = postContentTextView.text, !postContent.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).isEmpty
        else
    {
        self.postButton.tintColor = UIColor.lightGray
        self.postButton.isEnabled = false
        return
    }
    postButton.isEnabled = true
    postButton.tintColor = Colors.mainBlueColor
    }