Skip to content

Instantly share code, notes, and snippets.

@takoikatakotako
Created October 24, 2024 01:29
Show Gist options
  • Select an option

  • Save takoikatakotako/6ef8ff69185ee8ecb83f2d158e3d3e10 to your computer and use it in GitHub Desktop.

Select an option

Save takoikatakotako/6ef8ff69185ee8ecb83f2d158e3d3e10 to your computer and use it in GitHub Desktop.
SwiftUIでListViewからそれぞれ別のViewに遷移する
import SwiftUI
struct ContentView: View {
@State var pokemon: Pokemon?
var body: some View {
NavigationStack {
List (Pokemon.allCases) { pokemon in
Button {
self.pokemon = pokemon
} label: {
Text(pokemon.description)
}
}
.navigationDestination(item: $pokemon) { pokemon in
switch pokemon {
case .snorlax:
Image(.snorlax)
.resizable()
.scaledToFit()
case .slowpoke:
VStack {
Image(.slowpoke)
.resizable()
.scaledToFit()
Text("Snorlax")
}
case .ditto:
HStack {
Image(.ditto)
.resizable()
.scaledToFit()
Text("Ditto")
}
case .eevee:
Text("Eevee")
}
}
}
}
}
import Foundation
enum Pokemon: Identifiable, CaseIterable, CustomStringConvertible {
var id: Int {
self.hashValue
}
case snorlax
case slowpoke
case ditto
case eevee
var description : String {
switch self {
case .snorlax:
"Snorlax"
case .slowpoke:
"Slowpoke"
case .ditto:
"Ditto"
case .eevee:
"Eevee"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment