Skip to content

Instantly share code, notes, and snippets.

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

  • Save takoikatakotako/72f02f8874ba31b412212d5804b4024b to your computer and use it in GitHub Desktop.

Select an option

Save takoikatakotako/72f02f8874ba31b412212d5804b4024b to your computer and use it in GitHub Desktop.
SwiftUIでAlertを出し分ける
import Foundation
struct AlertItem {
let buttonTitle: String
let message: String
}
import SwiftUI
struct ContentView: View {
@State var showingAlert = false
@State var alertItem: AlertItem?
var body: some View {
VStack {
Button {
alertItem = AlertItem(buttonTitle: "Pikachu", message: "Pikachu is a fictional species in the Pokémon media franchise. ")
showingAlert = true
} label: {
Text("Pikachu")
}
Button {
alertItem = AlertItem(buttonTitle: "Snorlax", message: "Snorlax is most popular Pokémon.")
showingAlert = true
} label: {
Text("Snorlax")
}
}
.alert("Alert Title", isPresented: $showingAlert, presenting: alertItem) { alertItem in
Button(alertItem.buttonTitle, role: .none) {
print(alertItem.buttonTitle)
}
} message: { alertItem in
Text(alertItem.message)
}
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment