我正在尝试为iOS创建一个闹钟应用程序,我是Swift的新手。我想知道如何从UIDatePicker获得时间。直到现在,我有一个代码,它只给出了整个日期的时间。 `import UIKit
类ViewController:UIViewController {
@IBOutlet weak var alarmTime: UIDatePicker!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func alarmTime(_ sender: UIDatePicker){
sender.datePickerMode = UIDatePickerMode.time
print(sender.date)
}
} ` Here is the screenshot of the output of the above code in the terminal
答案 0 :(得分:6)
将日期选择器模式设置如下。
datePicker.datePickerMode = UIDatePickerMode.Time
从日期获取时间:
let date = Date()
let calendar = Calendar.current
let comp = calendar.dateComponents([.hour, .minute], from: date)
let hour = comp.hour
let minute = comp.minute
答案 1 :(得分:3)
let datePicker = UIDatePicker()
datePicker.datePickerMode = UIDatePickerMode.time
// datePicker.backgroundColor = UIColor.blue
txtFildDeadLine.textField.inputView = datePicker
根据您的问题,将其放在viewDidLoad
中
alarmTime.datePickerMode = UIDatePickerMode.time