老实说,我从来没有真正这样做过,所以我在这里非常迷失。我有一个应用程序,向用户展示一个UIPickerView
带有天:小时:分钟的应用程序。
我必须确保最短选择时间为 10 分钟,最长选择时间为 7 天。如果用户选择超过 7 天,则应将天数和小时数组件重置回 0。我还必须获取数据并将其显示在UILabel
选择器上方。这是我在代码方面得到的:
struct PollTime {
let time: Int
let description: String
}
struct PollModel {
let days: [PollTime]
let hours: [PollTime]
let minutes: [PollTime]
var currentTime: (day: PollTime, hour: PollTime, minute: PollTime)?
init() {
days = [Int](0...7).map { PollTime(time: $0, description: $0 == 0 || $0 > 1 ? "days" : "day") }
hours = [Int](0...23).map { PollTime(time: $0, description: $0 == 0 || $0 > 1 ? "hours" : "hour") }
minutes = [Int](0...59).map { PollTime(time: $0, description: $0 == 0 || $0 > 1 ? "min" : "min") }
}
func validateCurrenTime() -> Bool {
guard let currentTime = currentTime else { return false }
//if (//TODO: check to see if the currentTime falls within the specified time required.) {
// return true
// }
return false
}
}
var pollModel = PollModel()
extension PostPollVC : UIPickerViewDelegate,UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 3
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
switch component {
case 0:
return pollModel.days.count
case 1:
return pollModel.hours.count
case 2:
return pollModel.minutes.count
default:
return 0
}
}
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
return pickerView.frame.size.width / 3
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let currentDate = Date()
var dateComponents = DateComponents()
let days = pickerView.selectedRow(inComponent: 0)
let hours = pickerView.selectedRow(inComponent: 1)
let minutes = pickerView.selectedRow(inComponent: 2)
pollModel.currentTime = (PollTime(time: days, description: ""), PollTime(time: hours, description: ""), PollTime(time: minutes, description: ""))
if days != 0 && hours != 0 && minutes != 0 && !pollModel.validateCurrenTime() {
let alert = UIAlertController(title: "Invalid Time", message: "You must select a time greater than 10 minutes, up to 7 days.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { [weak self] action in
//TODO: Reset the components of the picker to one less than or great than the invalid choices....
})
self.present(alert, animated: true, completion: nil)
}
dateComponents.day = days
dateComponents.hour = hours
dateComponents.minute = minutes
let calendar = Calendar(identifier: .gregorian)
let futureDate = calendar.date(byAdding: dateComponents, to: currentDate)
print("The current date is: \(currentDate.description)")
print("Your future date is: \(futureDate?.description ?? "")")
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label = UILabel()
label.textColor = Colors.mainBlueColor
label.textAlignment = .center
label.font = UIFont(name: Fonts.OpenSans_Bold, size: 14)
switch component {
case 0:
label.text = "\(pollModel.days[row].time) \(pollModel.days[row].description)"
return label
case 1:
label.text = "\(pollModel.hours[row].time) \(pollModel.hours[row].description)"
return label
case 2:
label.text = "\(pollModel.minutes[row].time) \(pollModel.minutes[row].description)"
return label
default:
return label
}
}
}