Skip to content

Instantly share code, notes, and snippets.

@PaulWoodIII
Created August 3, 2019 22:59
Show Gist options
  • Select an option

  • Save PaulWoodIII/7f6a487fab3d8514e3c51be4d9a2af58 to your computer and use it in GitHub Desktop.

Select an option

Save PaulWoodIII/7f6a487fab3d8514e3c51be4d9a2af58 to your computer and use it in GitHub Desktop.
An example of a complex SwiftUI Alert view using and Identifiable binding
//: [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