How to get an alert when you tap a cell in a list in SwiftUI. I still haven't gotten used to SwiftUI yet.
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 | |
fileprivate struct Pokemon: Identifiable { | |
let id = UUID() | |
let name: String | |
} | |
struct ContentView: View { | |
@State private var pokemons: [Pokemon] = [ | |
Pokemon(name: "カビゴン"), | |
Pokemon(name: "ピカチュー"), | |
Pokemon(name: "コダック"), | |
Pokemon(name: "カメックス"), | |
Pokemon(name: "ヤドン")] | |
@State private var showingAlert = false | |
var body: some View { | |
List(pokemons) { pokemon in | |
Button(action: { | |
self.showingAlert = true | |
}, label: { | |
Text(pokemon.name) | |
}).alert(isPresented: self.$showingAlert) { | |
Alert(title: Text("ポケモン名"), message: Text(pokemon.name), dismissButton: .default(Text("閉じる"))) | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |