Created
August 3, 2019 22:59
-
-
Save PaulWoodIII/7f6a487fab3d8514e3c51be4d9a2af58 to your computer and use it in GitHub Desktop.
An example of a complex SwiftUI Alert view using and Identifiable binding
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
| //: [Previous](@previous) | |
| import SwiftUI | |
| import PlaygroundSupport | |
| enum AlertTypes: String, Identifiable { | |
| case simple = "A Simple Alert" | |
| case complex = "A Complex Alert" | |
| var id: String { | |
| return self.rawValue | |
| } | |
| static func alert(for type: AlertTypes) -> Alert { | |
| switch type { | |
| case .simple: | |
| return Alert(title: Text(type.rawValue), | |
| message: Text("Alert Message"), | |
| dismissButton: .default(Text("Dismiss"))) | |
| case .complex: | |
| return Alert(title: Text(type.rawValue), | |
| message: Text("Alert Message with a large amount of text so it will wrap to more than one line"), | |
| primaryButton: .default(Text("Dismiss"), action: { | |
| print("dismiss button pressed") | |
| }), | |
| secondaryButton: .destructive(Text("Destroy something"), action: { | |
| print("Destroy something pressed") | |
| }) | |
| ) | |
| } | |
| } | |
| } | |
| struct IdentifiableAlertView : View { | |
| @State var alert: AlertTypes? = nil | |
| var body: some View { | |
| VStack { | |
| Button("Simple Alert") { | |
| self.alert = .simple | |
| }.padding() | |
| Button("Complex Alert") { | |
| self.alert = .complex | |
| }.padding() | |
| }.alert(item: $alert, content: AlertTypes.alert(for:)) | |
| } | |
| } | |
| let viewController = UIHostingController(rootView: IdentifiableAlertView()) | |
| PlaygroundPage.current.liveView = viewController | |
| //: [Next](@next) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment