【发布时间】:2021-01-08 02:22:43
【问题描述】:
我试图弄清楚如何在 SwiftUI 中按下按钮时更改字符串的文本。
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
let title = Text("Button Not Clicked")
.font(.title)
.fontWeight(.heavy)
.foregroundColor(.red)
.padding()
.frame(height: 0.0)
}
Button(action: {
title.text = "Button Clicked!"
}) {
Text("Click Here")
}
}
}
到目前为止,我尝试使用 'title.text = (string)' 更改字符串,但这不起作用。有什么想法吗?
【问题讨论】: