2017-06-22 57 views
3

在我的項目中添加日期和月份 如何在(日期和時間)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) 
    } 

輸出是:

[圖像缺失]

我已預料到這樣的輸出:

enter image description here

+0

什麼是當前的輸出? –

+0

其顯示只有一個字段(月) – Ramprasathselvam

+0

但我期望輸出添加兩個字段(天和月)在pickerView – Ramprasathselvam

回答

0
class TableViewController: UITableViewController ,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource{ 

@IBOutlet var dateTextfield:UITextField! 
    @IBOutlet var dateLabel:UILabel! 
    let datePicker = UIPickerView() 
override func viewDidLoad() { 
     super.viewDidLoad() 
     self.dateTextfield.delegate = self 

     self.datePicker.dataSource = self 
     self.datePicker.delegate = self 

     self.dateTextfield.inputView = self.datePicker 

     let toolBar = UIToolbar() 
     toolBar.barStyle = UIBarStyle.default 
     toolBar.isTranslucent = true 
     toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1) 
     toolBar.sizeToFit() 
     let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.doneButtonTapped)) 
     toolBar.setItems([doneButton], animated: false) 
     self.dateTextfield.inputAccessoryView = toolBar 
} 
func doneButtonTapped(){ 
     self.dateTextfield.resignFirstResponder() 
    } 
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     view.endEditing(true) 
    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

//---------------- PickerView delegate Method --------------------------------// 

    func numberOfComponents(in pickerView: UIPickerView) -> Int { 
     return 2 
    } 
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
     return self.pickerDaysmonth[component].count 
    } 
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
     return self.pickerDaysmonth[component][row] 
    } 
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 

     let days = pickerDaysmonth[0][pickerView.selectedRow(inComponent: 0)] 
     let month = pickerDaysmonth[1][pickerView.selectedRow(inComponent: 1)] 
     dateTextfield.text = days + " " + month 

    } 

分配inputview爲文本框在UIPickerView()

let datePicker = UIPickerView() 

self.dateTextfield.inputView = self.datePicker 

分配inputAccessoryView在工具欄上文本框

let toolBar = UIToolbar() 
    self.dateTextfield.inputAccessoryView = toolBar 

它的正常工作

電流輸出是:

enter image description here

1

功能numberOfComponentsnumberOfRowsInComponent從數據源,而不是代表。

所以你需要這行添加到您的viewDidLoad

self.pickerview.delegate = self

+0

它的工作很好謝謝 – Ramprasathselvam

+0

@RamprasathS請標記爲正確的它解決了你的問題,或者讓我知道是否有其他問題。謝謝 –