在 UITextView 和 UITextField 中输入内容后未启用 UIBarButtonItem
UIBarButtonItem not being enabled after entered content in UITextView & UITextField
我有一个 UIViewController
和 3 个 UITextFields
和一个 UITextView
。我需要的强制性内容是称为 postTitleTextField
的 UITextFields
或在启用类型为 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
}
}
我自己想出来了。基本上,我必须改变两件事:
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
}
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
}
我有一个 UIViewController
和 3 个 UITextFields
和一个 UITextView
。我需要的强制性内容是称为 postTitleTextField
的 UITextFields
或在启用类型为 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
}
}
我自己想出来了。基本上,我必须改变两件事:
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 }
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 }