I've created a text and I would like that the text will change on the press of the button. For example: there's a Text() witch now has written on it "text" and when I press the blue button (you can see it in my code), I want "text" to change to number "5"
Thanks for showing your code. You just need to declare an @State variable to hold the content of the Text.
(Defining a struct NumberView makes your code too complex, better remove it.)
import SwiftUI //<-
import PlaygroundSupport
let columns: [GridItem] = [GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible())]
struct ContentView: View {
@State var outputText: String = "text" //<-
var body: some View {
GeometryReader { geometry in
VStack {
Spacer(minLength: 290)
Text(outputText) //<-
.font(.largeTitle)
ZStack {
LazyVGrid(columns: columns) {
Group {
Button {
outputText = "\(5)" //<-
} label: {
ZStack {
Circle()
.frame(width: 90, height: 90)
.foregroundColor(Color(.systemBlue))
.opacity(0.5)
//.position(x: geometry.size.width/2, y: -100)
Text("5")
.font(.largeTitle)
.bold()
.foregroundColor(.black)
}
.frame(width: 90, height: 90)
}
}
}
}
}
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: