SwiftUIでモーダルを表示する時に値を渡す
SwiftUIでモーダルを表示する時に値を渡す方法です。 TextFieldでname変数に文字列を入力します。その入力した文字列をPokemonView に渡して表示しています。
This file contains 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 name = "" | |
@State var showingModal = false | |
var body: some View { | |
VStack(spacing: 16) { | |
TextField("Input Name", text: $name) | |
.textFieldStyle(RoundedBorderTextFieldStyle()) | |
.padding() | |
Text("Name: \(name)") | |
Button { | |
showingModal = true | |
} label: { | |
Text("Show Modal") | |
.font(Font.system(size: 20)) | |
.foregroundColor(Color.white) | |
.padding(16) | |
.background(Color.gray) | |
.cornerRadius(16) | |
} | |
.sheet(isPresented: $showingModal) { | |
PokemonView(pokemonName: name) | |
} | |
} | |
} | |
} | |
struct PokemonView: View { | |
let pokemonName: String | |
var body: some View { | |
Text("Name: \(pokemonName)") | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |