Reputation: 17
A textview writes "You have meditated 0 times today." i've connected it through an IBOutlet.
Is it possible to increment just the "0" after a code has been completed, and update the text?
Upvotes: 1
Views: 494
Reputation: 874
The best approach, updating textView in the counter didSet:
var mediatationOfDay = 0 {
didSet {
myTextView.text = "You have meditated \(mediatationOfDay) times today."
}
}
To answer your initial question, it's possible, but I would not recommend it:
var mediatationOfDay = 0
var textValue = "You have meditated 0 times today."
private func incrementMedidationOfTheDay() {
let oldvalue = mediatationOfDay
mediatationOfDay += 1
textValue = textValue.replacingOccurrences(of: oldvalue.description, with: mediatationOfDay.description)
}
Also possible:
private func incrementMedidationOfTheDay() {
mediatationOfDay += 1
myTextView.text = "You have meditated \(mediatationOfDay) times today."
}
Upvotes: 1
Reputation: 17
you can set it programmatically
@IBOutlet weak var yourTxtView: UITextView!
yourTxtView.text = "You have meditated \(count) times today."
Upvotes: -1
Reputation: 270980
You should use a variable to store the number:
var timesMeditated = 0 {
didSet {
yourTextView.text = "You have meditated \(timesMeditated) times today"
}
}
Note how I wrote the didSet
property observer. That will be called when the variable is changed, so whenever you do this:
timesMeditated += 1
Your text view will update as well.
Upvotes: 5
Reputation: 100503
Try this
self.textView.text = "You have meditated \(counter) times today."
Upvotes: 0
Reputation: 16426
you can just set whole text again with new text
like
yourTextView.text = "You have meditated \(counter) times today."
counter
is your variable which contains count
Upvotes: 0