UIPicker 作为 UITextfield 键盘的输入
Posted
技术标签:
【中文标题】UIPicker 作为 UITextfield 键盘的输入【英文标题】:UIPicker as input for UITextfield keyboard 【发布时间】:2015-03-16 20:58:43 【问题描述】:最初我想在选择 UITableViewCell 或 UILabel 时实现一个选择器。
最好的方法是 1. 继承 UItextfield 并将其 inputView 设置为选择器。 2. 使该文本字段成为 didSelectRowAtIndexPath 中的FirstResponder
下面回答
【问题讨论】:
【参考方案1】:inputView
属性用于将选取器视图分配给 TextField 的第一响应者。它不会显示 PickerView。因此,WarmUpTextField.inputView = Picker
应该移动到viewDidLoad()
。
您对didSelectRowAtIndexPath
的期望是将 TextField 设置为第一响应者。下面这个函数的代码应该像你期望的那样工作。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
if indexPath.section == 0
NameTexField.becomeFirstResponder()
else if indexPath.section == 1
WarmUpTextField.becomeFirstResponder()
【讨论】:
【参考方案2】:最好的办法是继承你的 UITextfield 并执行以下操作:
class PickerTextField: UITextField, UIPickerViewDataSource, UIPickerViewDelegate
var picker:UIPickerView = CustomUIPickerView()
var pickerHours: Int = 0
var pickerMinutes: Int = 0
var pickerSeconds: Int = 0
var pickerTotal: Int = 0
override func awakeFromNib()
super.awakeFromNib()
picker.delegate = self
picker.dataSource = self
override func layoutSubviews()
super.layoutSubviews()
(pickerHours,pickerMinutes,pickerSeconds) = MakeTimeFromString(self.text!)
self.inputView = configurePicker()
self.inputAccessoryView = configureAccessoryView()
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
// Prevent textfield from editing
return false
func configurePicker() -> UIView
picker.selectRow(pickerHours, inComponent: 0, animated: true)
picker.selectRow(pickerMinutes, inComponent: 1, animated: true)
picker.selectRow(pickerSeconds, inComponent: 2, animated: true)
return picker
func configureAccessoryView() -> UIView
let inputAccessoryView = UIToolbar(frame: CGRectMake(0.0, 0.0, UIScreen.mainScreen().bounds.size.width, 44))
inputAccessoryView.barStyle = UIBarStyle.BlackTranslucent
let flex = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil)
// Configure done button
let doneButton = UIBarButtonItem()
doneButton.title = "Done"
doneButton.tintColor = UIColor.greenColor()
doneButton.action = Selector("dismissPicker")
inputAccessoryView.items = NSArray(array: [flex, doneButton]) as? [UIBarButtonItem]
return inputAccessoryView
// Disallow selection or editing and remove caret
override func caretRectForPosition(position: UITextPosition) -> CGRect
return CGRectZero
override func selectionRectsForRange(range: UITextRange) -> [AnyObject]
return []
override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool
UIMenuController.sharedMenuController().menuVisible = false
if action == "copy:" || action == "selectAll:" || action == "paste:"
return false
return super.canPerformAction(action, withSender:sender)
func dismissPicker ()
self.resignFirstResponder()
//MARK: - UIPickerView Functions
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
return 3
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
if component == 0
return 24
return 60
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString?
let title = String(row)
let attributedString = NSAttributedString(string: title, attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])
pickerView.backgroundColor = UIColor.clearColor()
return attributedString
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
if component == 0
pickerHours = row
else if component == 1
pickerMinutes = row
else if component == 2
pickerSeconds = row
pickerTotal = AssembleTimeFromComponents(pickerHours, minutesComponent: pickerMinutes, secondsComponent: pickerSeconds)
UpdateLabel()
func UpdateLabel()
self.text = MakeTimeLabel(pickerTotal, type: "Routine")
self.sizeToFit()
对于更通用的库,请尝试: https://github.com/gtsif21/UIKeyboardLikePickerTextField
【讨论】:
以上是关于UIPicker 作为 UITextfield 键盘的输入的主要内容,如果未能解决你的问题,请参考以下文章
UIScrollView 使 UITextField 停止调用 UIPicker
如何为 iOS 7 创建弹出式 uipicker 和 uitextfield?