在我的項目中添加日期和月份 如何在(日期和時間)UIPickerView中添加兩個字段。 在我的代碼是「UIPickerView」中的多列
class TableViewController: UITableViewController ,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource{
@available(iOS 2.0, *)
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
@IBOutlet var dateTextfield:UITextField!
@IBOutlet var dateLabel:UILabel!
var pickerDaysYear = [["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"],["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]];
override func viewDidLoad() {
super.viewDidLoad()
self.pickerview.delegate = self
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 2
}
numberOfRowsInComponent component: Int) -> Int {
return self.pickerDaysYear[component].count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if(component == 0)
{
return self.pickerDaysYear[component][row]
}
else
{
return self.pickerDaysYear[component][row]
}
// return pickerDaysYear[component][row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let days = pickerDaysYear[0][pickerView.selectedRow(inComponent: 0)]
let month = pickerDaysYear[1][pickerView.selectedRow(inComponent: 1)]
dateTextfield.text = days + " " + month
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(false)
}
輸出是:
[圖像缺失]
我已預料到這樣的輸出:
什麼是當前的輸出? –
其顯示只有一個字段(月) – Ramprasathselvam
但我期望輸出添加兩個字段(天和月)在pickerView – Ramprasathselvam