当我们单击按钮时,值将在屏幕上更新
import SwiftUI
struct ContentView: View {
@State private var name = ""
var body: some View {
VStack{
Button("click me",role: .destructive, action: Clicked).buttonStyle(.borderedProminent).tint(.green).foregroundColor(.black)
Text("Hello \(name)")
}
}
func Clicked(){
name = "Saurabh"
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
输出: