Skip to content

Commit 9d333c9

Browse files
committed
Update documentation of OptionPickerControl
1 parent 5cfe4a8 commit 9d333c9

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## `develop`
2+
3+
* Drop Class Name Prefixes
4+
* Add an easy to use `OptionPickerControl` that displays a `UIPickerView` with given options
5+
16
## v1.5.0
27

38
* Swift 4.0

Example/Language.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ enum Language: String, OptionDescriptive {
5151
return rawValue.capitalized
5252
}
5353

54-
static var optionalText: String {
54+
static var titleForOptionalValue: String {
5555
return "(Optional)"
5656
}
5757

Source/OptionPickerControl/Option.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// Option.swift
3-
// Flashcards
3+
// ICInputAccessory
44
//
55
// Created by Ben on 22/11/2017.
66
// Copyright © 2017 bcylin.
@@ -26,32 +26,38 @@
2626

2727
import Foundation
2828

29+
/// The protocol defines the required variables to be displayed in a `UIPickerView` via `OptionPickerControl`.
2930
public protocol OptionDescriptive: Equatable {
31+
/// The text for the row in the `UIPickerView`.
3032
var title: String { get }
31-
static var optionalText: String { get }
33+
/// The text for a placeholder row when the picker selection is optional.
34+
static var titleForOptionalValue: String { get }
3235
}
3336

34-
////////////////////////////////////////////////////////////////////////////////
35-
3637

38+
/// An option struct that carries the `OptionDescriptive`
3739
public struct Option<T: OptionDescriptive>: Equatable {
3840

3941
// MARK: - Initialization
4042

43+
/// Returns an option that displays the optional value of an `OptionDescriptive` type.
4144
public static func optional() -> Option<T> {
4245
return Option<T>()
4346
}
4447

48+
/// Returns an initialized option with an instance of an `OptionDescriptive` type.
4549
public init(_ value: T) {
4650
self.value = value
4751
}
4852

4953
// MARK: - Properties
5054

55+
/// Conformance to `OptionDescriptive`.
5156
public var title: String {
52-
return value?.title ?? T.optionalText
57+
return value?.title ?? T.titleForOptionalValue
5358
}
5459

60+
/// The `OptionDescriptive` value of the option. Returns `nil` when it's the `optional` placeholder.
5561
public let value: T?
5662

5763
// MARK: - Private
@@ -62,6 +68,7 @@ public struct Option<T: OptionDescriptive>: Equatable {
6268

6369
// MARK: - Equatable
6470

71+
/// Returns true when two options' values are equal.
6572
public static func == (lhs: Option<T>, rhs: Option<T>) -> Bool {
6673
return lhs.value == rhs.value
6774
}

Source/OptionPickerControl/OptionPickerControl.swift

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// OptionPickerControl.swift
3-
// Flashcards
3+
// ICInputAccessory
44
//
55
// Created by Ben on 27/11/2017.
66
// Copyright © 2017 bcylin.
@@ -26,24 +26,28 @@
2626

2727
import UIKit
2828

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 {
3231

3332
// MARK: - Initialization
3433

34+
/// Returns an initialized `OptionPickerControl`.
3535
public init() {
3636
super.init(frame: .zero)
3737
addSubview(hiddenTextField)
3838
}
3939

40+
/// Not supported. `OptionPickerControl` is not compatible with storyboards.
4041
required public init?(coder aDecoder: NSCoder) {
4142
fatalError("init(coder:) is not supported")
4243
}
4344

4445
// MARK: - Properties
4546

47+
/// Options that shows in the `UIPickerView`.
4648
public var options: [Option<T>] = [Option<T>.optional()]
49+
50+
/// The currently selected item in the options.
4751
public var selectedOption: Option<T> = Option<T>.optional() {
4852
didSet {
4953
if hiddenTextField.isFirstResponder {
@@ -54,6 +58,14 @@ open class OptionPickerControl<T: OptionDescriptive>: UIControl,
5458
}
5559
}
5660

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+
5769
// MARK: - Lazy Instantiation
5870

5971
private lazy var doneBarButton: UIBarButtonItem =
@@ -68,13 +80,6 @@ open class OptionPickerControl<T: OptionDescriptive>: UIControl,
6880
return toolbar
6981
}()
7082

71-
private lazy var picker: UIPickerView = {
72-
let picker = UIPickerView()
73-
picker.dataSource = self
74-
picker.delegate = self
75-
return picker
76-
}()
77-
7883
private lazy var hiddenTextField: UITextField = {
7984
let textField = UITextField()
8085
textField.inputAccessoryView = self.pickerToolbar
@@ -96,6 +101,7 @@ open class OptionPickerControl<T: OptionDescriptive>: UIControl,
96101

97102
// MARK: - UIPickerViewDataSource
98103

104+
/// Currently `OptionPickerControl` only supports one component.
99105
open func numberOfComponents(in pickerView: UIPickerView) -> Int {
100106
return 1
101107
}

0 commit comments

Comments
 (0)