1
1
//
2
2
// OptionPickerControl.swift
3
- // Flashcards
3
+ // ICInputAccessory
4
4
//
5
5
// Created by Ben on 27/11/2017.
6
6
// Copyright © 2017 bcylin.
26
26
27
27
import UIKit
28
28
29
- open class OptionPickerControl < T: OptionDescriptive > : UIControl ,
30
- UIPickerViewDataSource ,
31
- UIPickerViewDelegate {
29
+ /// A `UIControl` that displays a `UIPickerView` and notifies changed selection and via `UIControlEvents` `.valueChanged`.
30
+ open class OptionPickerControl < T: OptionDescriptive > : UIControl , UIPickerViewDataSource , UIPickerViewDelegate {
32
31
33
32
// MARK: - Initialization
34
33
34
+ /// Returns an initialized `OptionPickerControl`.
35
35
public init ( ) {
36
36
super. init ( frame: . zero)
37
37
addSubview ( hiddenTextField)
38
38
}
39
39
40
+ /// Not supported. `OptionPickerControl` is not compatible with storyboards.
40
41
required public init ? ( coder aDecoder: NSCoder ) {
41
42
fatalError ( " init(coder:) is not supported " )
42
43
}
43
44
44
45
// MARK: - Properties
45
46
47
+ /// Options that shows in the `UIPickerView`.
46
48
public var options : [ Option < T > ] = [ Option< T> . optional( ) ]
49
+
50
+ /// The currently selected item in the options.
47
51
public var selectedOption : Option < T > = Option< T> . optional( ) {
48
52
didSet {
49
53
if hiddenTextField. isFirstResponder {
@@ -54,6 +58,14 @@ open class OptionPickerControl<T: OptionDescriptive>: UIControl,
54
58
}
55
59
}
56
60
61
+ /// A reference to the displayed `UIPickerView` for customization.
62
+ public private( set) lazy var picker : UIPickerView = {
63
+ let picker = UIPickerView ( )
64
+ picker. dataSource = self
65
+ picker. delegate = self
66
+ return picker
67
+ } ( )
68
+
57
69
// MARK: - Lazy Instantiation
58
70
59
71
private lazy var doneBarButton : UIBarButtonItem =
@@ -68,13 +80,6 @@ open class OptionPickerControl<T: OptionDescriptive>: UIControl,
68
80
return toolbar
69
81
} ( )
70
82
71
- private lazy var picker : UIPickerView = {
72
- let picker = UIPickerView ( )
73
- picker. dataSource = self
74
- picker. delegate = self
75
- return picker
76
- } ( )
77
-
78
83
private lazy var hiddenTextField : UITextField = {
79
84
let textField = UITextField ( )
80
85
textField. inputAccessoryView = self . pickerToolbar
@@ -96,6 +101,7 @@ open class OptionPickerControl<T: OptionDescriptive>: UIControl,
96
101
97
102
// MARK: - UIPickerViewDataSource
98
103
104
+ /// Currently `OptionPickerControl` only supports one component.
99
105
open func numberOfComponents( in pickerView: UIPickerView ) -> Int {
100
106
return 1
101
107
}
0 commit comments