Yoope
Yoope

Reputation: 2185

how to update a label when user is typing into a textview?

Hey I'm new to Swift and having some problems with my code.

Suppose I have two objects:

IBOutlet var textView: NSTextView!
IBOutlet weak var label: NSTextField!

So I want "label" to displays the current number of characters that is in the textView.

But I'm not sure how to make the "label" update automatically whenever the text in the textView changes (when user types in/ deletes)?

Can anyone help me ?

Thanks

Upvotes: 1

Views: 1043

Answers (1)

Ramzi Khahil
Ramzi Khahil

Reputation: 5052

You can just register an "observer" object with UITextFieldTextDidChangeNotification (see documentation). In the callback of that observer object you change the labels text.

This blog post may interest you.


Edit

Reference for NSTextField, which actually functions the same. Just use textDidChange(_:).

Upvotes: 1

Related Questions