SwiftUIでモーダルを表示する時に値を渡す
SwiftUIでモーダルを表示する時に値を渡す方法です。

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 name = "" | |
@State var showingSheet = false | |
var body: some View { | |
VStack(spacing: 16) { | |
TextField("Input Name", text: $name) | |
.textFieldStyle(RoundedBorderTextFieldStyle()) | |
.padding() | |
Text("Name: \(name)") | |
Button { | |
showingSheet = true | |
} label: { | |
Text("Show Modal") | |
.font(Font.system(size: 20)) | |
.foregroundColor(Color.white) | |
.padding(16) | |
.background(Color.gray) | |
.cornerRadius(16) | |
} | |
.sheet(isPresented: $showingSheet) { | |
PokemonView(pokemonName: name) | |
} | |
} | |
} | |
} | |
#Preview { | |
ContentView() | |
} |
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 PokemonView: View { | |
let pokemonName: String | |
var body: some View { | |
Text("Name: \(pokemonName)") | |
} | |
} |