Last active
July 13, 2018 15:01
-
-
Save popmedic/291af6918ac4f0a43d187e2379484c64 to your computer and use it in GitHub Desktop.
PickerTextField - Use PickerTextField when you want a list of choices in a UIPickerView displayed in place of the key board.
This file contains hidden or 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
import UIKit | |
/** | |
PickerTextField - Use PickerTextField when you want a list of choices in a UIPickerView displayed in place of the key board. | |
- Note: | |
Set it up by setting the choices to an array of strings. These are the strings that will show up in the UIPickerView for the key board. | |
- Note: | |
You can set a block for onSelected for action when something is selected. | |
- Note: | |
The selectedTimeOut property can be set for the amount of time to wait after something is selected before ending the edit. | |
*/ | |
public class PickerTextField: UITextField, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate { | |
/// choices - Array of strings to show in the UIPickerView that comes up for the keyboard. | |
public var choices = [String]() | |
/// currentRow - current selected row | |
public var currentRow:Int { | |
get { | |
return _currentRow | |
} | |
} | |
/// selectedTimeOut - how long to wait till ending edit when something is selected | |
public var selectedTimeOut:NSTimeInterval = 1.0 | |
required public init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
self._initialize() | |
} | |
/** | |
onSelect is a callback one can set for when a value is selected | |
set it to a function of type: (row:Int)->Void | |
*/ | |
public var onSelected:((row:Int)->Void)? = nil | |
override public var text: String? { | |
get { | |
return super.text | |
} | |
set(value) { | |
if let value = value { | |
if self.choices[self.currentRow] != value { | |
if let index = self.choices.indexOf(value) { | |
self._currentRow = index | |
} | |
} | |
if let selected = self.onSelected { | |
selected(row: self.currentRow) | |
} | |
} | |
super.text = value | |
} | |
} | |
/// Private function for common initialization | |
private func _initialize(){ | |
self._pickerView.delegate = self | |
self._pickerView.dataSource = self | |
self.inputView = self._pickerView | |
self.delegate = self | |
self.textAlignment = NSTextAlignment.Center | |
} | |
public func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { | |
return 1 | |
} | |
public func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { | |
return choices.count | |
} | |
public func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { | |
if let timer = self._selectedTimer { | |
timer.invalidate() | |
self._selectedTimer = nil | |
} | |
if row < choices.count { | |
return choices[row] | |
} | |
return "UNKNOWN" | |
} | |
public func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { | |
self._currentRow = row | |
self.text = choices[row] | |
if let timer = self._selectedTimer { | |
timer.invalidate() | |
self._selectedTimer = nil | |
} | |
self._selectedTimer = NSTimer.scheduledTimerWithTimeInterval(self.selectedTimeOut, target: self, selector: #selector(_selectedTimerFired), userInfo: nil, repeats: false) | |
} | |
public func textFieldDidBeginEditing(textField: UITextField) { | |
if let select = choices.indexOf(textField.text!) { | |
self._pickerView.selectRow(select, inComponent: 0, animated: false) | |
} | |
} | |
private var _pickerView:UIPickerView = UIPickerView() | |
internal var _selectedTimer:NSTimer? = nil | |
@objc private func _selectedTimerFired() -> Void { | |
self.endEditing(false) | |
} | |
private var _currentRow = 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment