假设我已经创建了文本视图。以后如何在代码中更改其文本?我可以使用@State属性作为“文本”视图的源吗?
struct ContentView: View {
var body: some View {
Text("Hello World!")
.onTapGesture {
// How can I change its text?
}
}
}
是的,可以!试试这个:
struct ContentView: View {
@State var text = "Hallo"
var body: some View {
Text(text)
.onTapGesture {
self.text = "changed"
}
}
}