Reputation: 129
Let's say I have created a Text view. How can I change its text later in the code? Can I use a @State property as the source for the Text view?
struct ContentView: View {
var body: some View {
Text("Hello World!")
.onTapGesture {
// How can I change its text?
}
}
}
Upvotes: 1
Views: 1400
Reputation: 8091
yes you can! try this:
struct ContentView: View {
@State var text = "Hallo"
var body: some View {
Text(text)
.onTapGesture {
self.text = "changed"
}
}
}
Upvotes: 2