所以我一直在尝试在我设置的TableViewController中设置几个pickerviews作为不同文本字段的键盘。当我点击TextFields时,我想要打开pickerview,它会打开一个pickerview键盘,但它没有信息,当我点击普通文本字段时它不会打开键盘在任何人都可以请帮助我。
import UIKit
class AddProductTableViewController: UITableViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var productPicker: UITextField!
@IBOutlet weak var unitCost: UITextField!
@IBOutlet weak var total: UITextField!
@IBOutlet weak var City: UITextField!
@IBOutlet weak var statePicker: UITextField!
@IBOutlet weak var date: UITextField!
@IBAction func datePicker(sender: UITextField) {
let datePickerView: UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Date
sender.inputView = datePickerView
datePickerView.addTarget(self, action: Selector("datePickerValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)
}
var productPickerOption = []
var statePickerOption = []
override func viewDidLoad() {
super.viewDidLoad()
productPickerOption = ["Diesel","Gas"]
let productPickerView = UIPickerView()
productPickerView.delegate = self
productPicker.inputView = productPickerView
statePickerOption = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI","MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"]
let statePickerView = UIPickerView()
statePickerView.delegate = self
statePicker.inputView = statePickerView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
//the number of compontes(or "columns") that the picker view should display
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
//the number rows for the component
if(pickerView.tag == 1) {
return productPickerOption.count
}else if(pickerView.tag == 2){
return statePickerOption.count
}else{
return 0
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//the title of the picker view
if(pickerView.tag == 1) {
return "\(productPickerOption[row])"
}else if(pickerView.tag == 2) {
return "\(statePickerOption)"
}else {
return nil
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if(pickerView.tag == 1){
productPicker.text = productPickerOption[row] as? String
}else if(pickerView.tag == 2){
statePicker.text = statePickerOption[row] as? String
}
}
}
答案 0 :(得分:1)
只需在viewDidLoad
方法中添加:
productPickerView.dataSource = self
statePickerView.dataSource = self
同样以这种方式在pickerView中添加标签:
productPickerView.tag = 1
statePickerView.tag = 2
您的代码如下:
import UIKit
class SecondTableViewController: UITableViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var productPicker: UITextField!
@IBOutlet weak var unitCost: UITextField!
@IBOutlet weak var total: UITextField!
@IBOutlet weak var city: UITextField!
@IBOutlet weak var statePicker: UITextField!
@IBOutlet weak var date: UITextField!
@IBAction func datePicker(sender: UITextField) {
let datePickerView: UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Date
sender.inputView = datePickerView
datePickerView.addTarget(self, action: Selector("datePickerValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)
}
var productPickerOption = []
var statePickerOption = []
override func viewDidLoad() {
super.viewDidLoad()
productPickerOption = ["Diesel","Gas"]
let productPickerView = UIPickerView()
productPickerView.delegate = self
productPickerView.dataSource = self
productPickerView.tag = 1
productPicker.inputView = productPickerView
statePickerOption = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI","MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"]
let statePickerView = UIPickerView()
statePickerView.delegate = self
statePickerView.dataSource = self
statePickerView.tag = 2
statePicker.inputView = statePickerView
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
//the number of compontes(or "columns") that the picker view should display
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
//the number rows for the component
if(pickerView.tag == 1) {
return productPickerOption.count
}else if(pickerView.tag == 2){
return statePickerOption.count
}else{
return 0
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//the title of the picker view
if(pickerView.tag == 1) {
return "\(productPickerOption[row])"
}else if(pickerView.tag == 2) {
return "\(statePickerOption[row])" //add [row] here
}else {
return nil
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if(pickerView.tag == 1){
productPicker.text = productPickerOption[row] as? String
}else if(pickerView.tag == 2){
statePicker.text = statePickerOption[row] as? String
}
}
}