Skip to content

Instantly share code, notes, and snippets.

@diamantidis
Created October 30, 2021 10:01
Show Gist options
  • Save diamantidis/a1b0ef4ad7f7ac0271b32fafbe79ef77 to your computer and use it in GitHub Desktop.
Save diamantidis/a1b0ef4ad7f7ac0271b32fafbe79ef77 to your computer and use it in GitHub Desktop.
PickerField for medium post
import SwiftUI
struct PickerField: UIViewRepresentable {
// MARK: - Public properties
@Binding var selectionIndex: Int?
// MARK: - Initializers
init<S>(_ title: S, data: [String], selectionIndex: Binding<Int?>) where S: StringProtocol {
self.placeholder = String(title)
self.data = data
self._selectionIndex = selectionIndex
textField = PickerTextField(data: data, selectionIndex: selectionIndex)
}
// MARK: - Public methods
func makeUIView(context: UIViewRepresentableContext<PickerField>) -> UITextField {
textField.placeholder = placeholder
return textField
}
func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<PickerField>) {
if let index = selectionIndex {
uiView.text = data[index]
} else {
uiView.text = ""
}
}
// MARK: - Private properties
private var placeholder: String
private var data: [String]
private let textField: PickerTextField
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment