[英]Change variable in UITextView
文本視圖中寫道:“您今天進行了0次冥想。” 我已經通過IBOutlet連接了它。
完成代碼后,是否可以僅遞增“ 0”並更新文本?
您應該使用一個變量來存儲數字:
var timesMeditated = 0 {
didSet {
yourTextView.text = "You have meditated \(timesMeditated) times today"
}
}
請注意我是如何編寫didSet
屬性觀察器的。 更改變量時將調用該方法,因此無論何時執行此操作:
timesMeditated += 1
您的文本視圖也會更新。
您可以再次用新文本設置整個文本
喜歡
yourTextView.text = "You have meditated \(counter) times today."
counter
是包含計數的變量
嘗試這個
self.textView.text = "You have meditated \(counter) times today."
最好的方法是在計數器didSet中更新textView :
var mediatationOfDay = 0 {
didSet {
myTextView.text = "You have meditated \(mediatationOfDay) times today."
}
}
有可能回答您的第一個問題,但我不建議您這樣做:
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)
}
也可能:
private func incrementMedidationOfTheDay() {
mediatationOfDay += 1
myTextView.text = "You have meditated \(mediatationOfDay) times today."
}
您可以通過編程設置
@IBOutlet weak var yourTxtView: UITextView!
yourTxtView.text = "You have meditated \(count) times today."
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.