SwiftUIでButtonが押されたら文字を変える
SwiftUIでButtonが押されたら文字を変える方法です。
@State
が付与された変数を変えることで View 変えることができます。

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
@State var text = "Snorlax" | |
var body: some View { | |
VStack { | |
Text(text) | |
Button(action: { | |
text = "Forever" | |
}) { | |
Text("Tap Me!!") | |
} | |
} | |
} | |
} |