I\'m developing note app, when the text view is empty the done button should be disabled so user could not be able to save empty notes into data base, else the button should be
UITextViewDelegate
protocoldelegate
on the text view to your view controller.func textViewDidChange(textView: UITextView) {
if textView == self.textView {
self.doneButton.enabled = !textView.text.isEmpty
}
}
Try this in textViewDidChange
method:
yourBarButtonItem.isEnabled = !(yourTextField.text?.isEmpty ?? false)
Try to use another delegate method for you're purpose. This is example :
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var button: UIButton!
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
if (textView.text.isEmpty) {
button.enabled = false
}
}
}
extension ViewController: UITextViewDelegate {
func textView(textView: UITextView, range: NSRange, replacementText text: String) -> Bool
{
if (!textView.text.isEmpty) {
button.enabled = true
} else {
button.enabled = false
}
return true
}
}