Responding to a button tap
In this section, we will begin making a view responsive by examining the simplest case; that is, building a view that needs to respond to a simple user event, a button tap, and changing the view appearance in response.
Let’s start by trying to write the first thing that would intuitively come to mind; let’s change a variable’s value and have the view respond to that value change, as shown in the following code example:
import SwiftUI struct MyViewStruct: View { Â Â Â Â var name = "Mary" Â Â Â Â var body: some View { Â Â Â Â Â Â Â Â VStack(spacing: 20) { Â Â Â Â Â Â Â Â Â Â Â Â Text("Variables in structs are immutable") Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â .fontWeight(.heavy) Â Â Â Â Â Â Â Â Â Â Â ...