2
A
回答
7
的OP说,他正在试图重新在斯威夫特this answer。这个问题的答案是杂牌组装电脑(这取决于使用24小时格式,而不是真正解决问题丹麦环境),但在这里,你会怎么做,在斯威夫特:
var datePicker = UIDatePicker() // Although you probably have an IBOutlet
datePicker.datePickerMode = UIDatePickerMode.Time
datePicker.locale = NSLocale(localeIdentifier: "da_DK")
但这是一个杂牌 - 推出自己的UIPicker
!
UPDATE 的OP说,滚你自己是“麻烦” - 这其实真的很简单 - 这里有一个简单的方法来做到这一点(虽然一个更好的办法是继承UIPickerView
)...
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var myDatePicker: UIPickerView!
private var calendar = Calendar.autoupdatingCurrent
override func viewDidLoad() {
super.viewDidLoad()
self.time = Date() // updates the picker to "now"
}
// MARK: UIPickerViewDataSource
func numberOfComponents(in: UIPickerView) -> Int { return 2 }
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 { // Hours
return 24
}
return 12 // five minute intervals
}
// MARK: UIPickerViewDelegate
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 { // Hours
return String(format: "%02d", row)
}
return String(format: "%02d", row * 5)
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("picker changed, selected \(self.time)")
}
// MARK: Getting & setting the time
var time: Date {
get {
let unitFlags: Calendar.Unit = [.year, .day, .hour, .minute, .second]
var components = self.calendar.components(unitFlags, from: Date())
components.hour = self.myDatePicker.selectedRow(inComponent: 0)
components.minute = self.myDatePicker.selectedRow(inComponent: 1) * 5
components.second = 0
if let date = self.calendar.date(from: components) {
return date
}
return Date() // shouldn't get here
}
set {
var components = self.calendar.components([.hour, .minute], from: newValue)
if let hour = components.hour, let minute = components.minute {
self.myDatePicker.selectRow(hour, inComponent: 0, animated: true)
self.myDatePicker.selectRow(minute/5, inComponent: 1, animated: true)
}
}
}
}
相关问题
- 1. DatePicker在Swift中调用didSelectRowATIndexPath
- 2. 24小时数= 24个值
- 3. SWIFT 2.2 DatePicker中的最小和最大小时
- 4. AppCache 24小时?
- 5. Swift:12hr时间转换为24小时当设备在12小时的队友
- 6. 约24小时12小时
- 7. 在sqoop导入中使用24小时时间戳
- 8. 在R中使用大于24小时的时间值
- 9. 如何使用超过24小时
- 10. 我在做24小时制
- 11. 24小时DateTime在C#
- 12. 从24小时格式删除冒号格式时间在jquery datepicker
- 13. Moment.js 24小时时间格式,处理第24小时
- 14. 的UIDatePicker 24小时
- 15. SimpleDateFormat的24小时
- 16. Highcharts 24小时制
- 17. 过去24小时
- 18. FileMover 24小时 - Python
- 19. 24小时和uiDatePicker
- 20. 如何在24小时内扣除时间使用时刻
- 21. 如何在PHP中使用12小时和24小时之间的转换?
- 22. 我想在iOS上使用datePicker swift
- 23. 在android中获得24小时差异
- 24. 在fullcalendar中24小时格式
- 25. 如何在LINUX中阻塞24小时IP地址使用iptables
- 26. Xcode swift:如何禁用一个按钮,并在24小时后重新启用?
- 27. 使用javascript转换24小时格式为12小时格式
- 28. 如何使用java将12小时转换为24小时
- 29. 使用javascript转换12小时至24小时格式
- 30. 查找一段24小时(未尤其是最后24小时)
你会如何在Objective-C中做到这一点?你在Swift中尝试过什么? – Grimxn
我一直在使用这篇文章中显示的方法http://stackoverflow.com/questions/2140388/how-to-disable-am-pm-in-uidatepicker不知道如何携带,以迅速 – Prateek