我在一年中的所有月份製作一個pickerView,我想創建一個數組,當前月份在pickerView的開頭,接下來的一個月等等。我已經在所有的月份中填充了pickerView和數組,並且我也獲得了當前日期。如何製作當前月份和接下來幾個月的數組。
let Months = ["January","Febrary","March","April","May","June","July","August","September","October","November","December"]
@IBOutlet var DateLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
DateTime()
}
func DateTime() {
let time = DateFormatter()
let date = DateFormatter()
time.timeStyle = .medium
date.dateStyle = .full
print(time.string(from: Date()))
print(date.string(from: Date()))
//RiderTimeStampRequested = time.string(from: Date())
DateLabel.text = date.string(from: Date())
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return Months.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return Months[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print(Months[row])
}
僅供參考 - 不要硬編碼自己的月份名稱數組。從'DateFormatter monthSymbols'(或其變體之一)獲取它們。 – rmaddy