textview写道:“你今天已经冥想了0次。”我通过IBOutlet连接了它。
代码完成后是否可以仅增加“0”,并更新文本?
答案 0 :(得分:3)
您应该使用变量来存储数字:
var timesMeditated = 0 {
didSet {
yourTextView.text = "You have meditated \(timesMeditated) times today"
}
}
请注意我是如何编写didSet
属性观察者的。当变量发生变化时会调用它,所以每当你这样做时:
timesMeditated += 1
您的文字视图也会更新。
答案 1 :(得分:0)
您可以使用新文本再次设置整个文本
喜欢
yourTextView.text = "You have meditated \(counter) times today."
counter
是您的变量,其中包含计数
答案 2 :(得分:0)
试试这个
self.textView.text = "You have meditated \(counter) times today."
答案 3 :(得分:0)
最佳方法,更新计数器中的textView didSet :
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."
}
答案 4 :(得分:-1)
您可以通过编程方式设置
@IBOutlet weak var yourTxtView: UITextView!
yourTxtView.text = "You have meditated \(count) times today."