[英]Swift: how to show UIPickerView when a UITextField is tapped
In My iOS app there is a table where some cells contain a UITextField and I would like when that UITextField is tapped then its UIPiCkerView is selected and, after the selection the UIPickerView should disappear. 在我的iOS应用程序中,有一个表,其中一些单元格包含UITextField,我想当点击该UITextField时,选择其UIPiCkerView,然后在选择之后UIPickerView应该消失。 Following this tutorial I have written the following class representing a cell with a picker view: 在学习完本教程之后,我编写了以下类,表示带有选取器视图的单元格:
import Foundation
import UIKit
class PickerCell: UITableViewCell, UIPickerViewDataSource,UIPickerViewDelegate, UITextFieldDelegate {
@IBOutlet weak var label : UILabel!
@IBOutlet var myPicker: UIPickerView! = UIPickerView()
@IBOutlet weak var selectedData: UITextField!
var pickerData:[String]!
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.selectedData.text = self.pickerData[row]
self.myPicker.hidden = true;
}
func configurePickerCell(labelText:String, enabled:Bool, defaultValueIndex:Int) {
self.label.text = labelText
var labelFrame:CGRect = self.label.frame
labelFrame.size.height = Settings.labelHeight
self.label.frame = labelFrame
self.myPicker.delegate = self
self.myPicker.dataSource = self
self.selectedData.delegate = self
self.setEditable(enabled)
}
func setEditable(canEdit:Bool) {
if (canEdit) {
self.selectedData.userInteractionEnabled = true
self.label.highlighted = false
self.selectedData.layer.borderColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1).CGColor
}
else {
self.selectedData.userInteractionEnabled = false
self.label.highlighted = true
self.selectedData.layer.borderColor = UIColor(red: 176/255, green: 176/255, blue: 176/255, alpha: 1).CGColor
}
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
self.myPicker.hidden = false
return false
}
}
but I think I am missing something because when I tap on the UITextView the UIPickerView does not appear but the cursor is showed inside it. 但是我想我丢失了一些东西,因为当我点击UITextView时,不会出现UIPickerView,而是在其中显示了光标。 What am I missing? 我想念什么?
so first where is your data and the uiviewController ? 那么首先是您的数据和uiviewController在哪里? you pickerView is correct but where you appli ? 您pickerView是正确的,但您在哪里应用?
try start like this your class class ViewController: UIViewController , UIPickerViewDelegate, UIPickerViewDataSource , CLLocationManagerDelegate 尝试像这样开始您的类类ViewController:UIViewController,UIPickerViewDelegate,UIPickerViewDataSource,CLLocationManagerDelegate
and not "class PickerCell: UITableViewCell" you use a "tableViewCell" to create a pickerView this is not a good idea 而不是“类PickerCell:UITableViewCell”,而是使用“ tableViewCell”创建pickerView,这不是一个好主意
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.