Created
October 24, 2024 01:47
-
-
Save takoikatakotako/72f02f8874ba31b412212d5804b4024b to your computer and use it in GitHub Desktop.
SwiftUIでAlertを出し分ける
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 | |
| struct AlertItem { | |
| let buttonTitle: String | |
| let message: String | |
| } |
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 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