【问题标题】:UIBarButtonItem not being enabled after entered content in UITextView & UITextField在 UITextView 和 UITextField 中输入内容后未启用 UIBarButtonItem
【发布时间】:2018-05-28 13:11:58
【问题描述】:

我有一个 UIViewController 和 3 个 UITextFields 和一个 UITextView。我需要的强制内容要么是UITextFields,称为postTitleTextField,要么是在启用UIBarButtonItem 类型的postButton 之前唯一有内容的UITextView。我做错了什么,它没有按照我期望的方式工作。

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
    }
}

【问题讨论】:

    标签: swift uitextfield uitextview uibarbuttonitem


    【解决方案1】:

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

    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
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-25
      • 2011-01-26
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多