Buttonを押すとTextの文字が変わる
SwiftUIでButtonが押されたら文字の色を変える方法です。
ボタンが押されたタイミングで @State var text
を更新します。
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: { | |
self.text = "Forever" | |
}) { | |
Text("Tap Me!!") | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |