Created
October 24, 2024 01:29
-
-
Save takoikatakotako/6ef8ff69185ee8ecb83f2d158e3d3e10 to your computer and use it in GitHub Desktop.
SwiftUIでListViewからそれぞれ別の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 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") | |
| } | |
| } | |
| } | |
| } | |
| } |
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 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