Created
August 3, 2022 14:18
-
-
Save carlynorama/bd8ba233db19370001de12192941d45b to your computer and use it in GitHub Desktop.
Generates a picker when passed an Emum that conforms to PickerSuppiliable protocol
This file contains 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
// | |
// EnumPicker.swift | |
// DataViewer | |
// | |
// Created by Carlyn Maw on 8/1/22. | |
// | |
import SwiftUI | |
protocol PickerSuppliable:CaseIterable, Hashable { | |
var menuText:String { get } | |
static var labelText:String { get } | |
} | |
fileprivate enum TestEnum:PickerSuppliable{ | |
static var labelText: String = "TestEnum" | |
//var id:TestEnum { self } | |
case yes, no, maybe | |
var menuText: String { | |
switch self { | |
case .yes: | |
return "Yes" | |
case .no: | |
return "No" | |
case .maybe: | |
return "Maybe" | |
} | |
} | |
} | |
struct EnumPicker<E:PickerSuppliable>: View { | |
let options:[E] = E.allCases as! [E] | |
var label:String? = nil | |
@Binding var value:E | |
var body: some View { | |
VStack { | |
VStack { | |
Picker(label ?? E.labelText, selection: $value) { | |
//ForEach(options) { option in is only for identifiable | |
ForEach(options, id: \.self) { option in | |
Text(option.menuText) | |
} | |
} | |
} | |
} | |
} | |
} | |
struct EnumPicker_Previews: PreviewProvider { | |
static var previews: some View { | |
EnumPicker<TestEnum>(value: .constant(.maybe)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment