Using Swift UI, how to change a button title in the button coding preferable but anywhere would be ok. I have seen no reference in apple docs.
Button title change in Swift
An example on how to toggle the title of the button when tapping:
The buttonState could also be changed anywhere in code.
struct ContentView: View {
@State var buttonTapped = false
var body: some View {
Button(action: {
buttonTapped.toggle()
}) {
if buttonTapped {
Text("Second")
} else {
Text("First")
}
}
Text("Hello, world!")
.onTapGesture { // Can toggle title here as well
buttonTapped.toggle()
}
}
}