Reputation: 11
I have an app I am working on where it shows a balance for users that changes. I would like to display these values as they change and the only way I know how to do that would be with a button. But the values often change on a different view controller so I was wondering if I can set labels equal to a variable that update along with those variables.
Since the values change outside of the view controllers with the labels, the normal way I change labels, using a button, does not apply.
Thanks in advance!
Upvotes: 1
Views: 80
Reputation: 31665
As a general solution, you could achieve this by declaring a property observer
in your view controller, example:
class ViewController: UIViewController {
var updatedData = "" {
didSet {
lblData.text = "Data: \(updatedData)"
}
}
@IBOutlet weak var lblData: UILabel!
}
At this point, each time updatedData
value is edited, the lblData
label text will be updated.
Note that there is also willSet
option which is called just before the value is stored, for more information, you could check Swift Properties Documentation - Property Observers.
Upvotes: 1