Skip to content

Instantly share code, notes, and snippets.

@PaulWoodIII
Last active August 3, 2019 21:54
Show Gist options
  • Select an option

  • Save PaulWoodIII/1f694fa189c9dd36e129bd156f3b335b to your computer and use it in GitHub Desktop.

Select an option

Save PaulWoodIII/1f694fa189c9dd36e129bd156f3b335b to your computer and use it in GitHub Desktop.
Quick example of Identifiable Action Sheets using SwiftUI, one view may need many different action sheets and this is one implementation to handle that, and even share action sheet logic across your application
//: [Previous](@previous)
import SwiftUI
import UIKit
import PlaygroundSupport
struct IdentifiableActionSheet : View {
@State var displaySheet: SheetTypes? = nil
var body: some View {
VStack {
Button(
action: {
self.displaySheet = .simple
}, label: {
Text("Simple")
}).padding()
Button(
action: {
self.displaySheet = .titleDescription
}, label: {
Text("Title and Description")
}).padding()
Button(
action: {
self.displaySheet = .complex
}, label: {
Text("Complex Sheet")
}).padding()
}.actionSheet(item: $displaySheet, content: SheetTypes.actionSheet)
}
}
enum SheetTypes: String, Identifiable {
case simple
case titleDescription
case complex
static func actionSheet(for sheetType: SheetTypes) -> ActionSheet {
return ActionSheet(title: sheetType.title,
message: sheetType.message,
buttons: sheetType.buttons)
}
var title: Text {
return Text(self.rawValue)
}
var message: Text? {
switch self {
case .simple:
return nil
default:
return Text("A Shared Description")
}
}
var buttons: [ActionSheet.Button] {
switch self {
case .complex:
return [
.default(Text("OK"), action: {
print("OK Pressed")
}),
.cancel(Text("Forget about it"), action: {
print("forget about it pressed")
}),
.destructive(Text("Delete"), action: {
print("Delete Pressed")
})
]
default:
return [.cancel()]
}
}
var id: String {
return self.rawValue
}
}
let viewController = UIHostingController(rootView: IdentifiableActionSheet())
PlaygroundPage.current.liveView = viewController
//: [Next](@next)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment