UIPickerView 选择运行代码的时间 [英] UIPickerView selected time to run code
问题描述
我添加了一个 UIPickerView 并且当前将选定的时间存储为字符串.我希望应用程序在选择器视图上选择的时间是现实世界中的时间时执行一行简单的代码.这是我添加的代码.
I have added in a UIPickerView and currently have it store the selected time as a string. I want the app to carry out a simple line of code when the time that was selected on the pickerview is the time in the real world. Here is the code that I have added.
对于时钟,用于查找现实世界的时间:
For the Clock, used to find the real world time:
let clockString: String = formatADate()
func formatADate() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.dateFormat = "hh:mm:ss a"
let date = NSDate()
let output = dateFormatter.string(from: date as Date)
print(output)
return output
}
这是 UIPickerView 的代码:
Here is the code for the UIPickerView:
@IBOutlet var dateTimeDisplay: UILabel!
@IBOutlet var datePicker: UIDatePicker!
@IBAction func datePickerchanged(_ sender: Any) {
setDateAndTime()
}
func setDateAndTime() {
let formatter = DateFormatter()
formatter.dateFormat = "hh:mm:ss a"
_ = formatter.string(from: datePicker.date)
str = dateFormatter.string(from: (datePicker?.date)!)
dateTimeDisplay.text = str
}
这是当所选时间和现实世界时间匹配时我想要发生的事情:
And here is what I want to happen when the selected time and the real world time match up:
takePhoto = true
推荐答案
When the pick date the start one timer function
When the pick date the start one timer function
调用picker中的函数
call the function in picker
var timercount = Timer()
viewdidload()
{
timercount = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(Check), userInfo: nil, repeats: true)
}
Func Check()
{
let nowdate = NSDate()//two date declare global Var
let date2 = datePicker?.date //chek the Time How Much Time Remain
let elapsed = date2?.timeIntervalSince(nowdate as Date)
if Int(elapsed!) == 0
{
takePhoto = true
}
}
这篇关于UIPickerView 选择运行代码的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!