gpt4 book ai didi

swift - timePicker 的 Action 监听器

转载 作者:行者123 更新时间:2023-11-30 11:16:55 26 4
gpt4 key购买 nike

我有一个 timePicker,如果用户更改 textFiled 上的时间字符串集,我已经声明了一个监听器。

func timeFromAction(){

DispatchQueue.main.async {

self.timePickerFrom.datePickerMode = UIDatePickerMode.time
//self.timePickerFrom.minimumDate = self.minDate
print(self.minDate)
self.timePickerFrom.date = self.minDate // current date I have achieve from millisecond to date

self.timePickerFrom.locale = Locale(identifier: "fa_IR")
self.timePickerFrom.calendar = Calendar(identifier: Calendar.Identifier.persian)

self.timePickerFrom.timeZone = TimeZone(abbreviation: "IRST")

self.timePickerFrom.frame=CGRect(x: 0, y: 0, width: 288, height: 83)

self.timeOfLoading.addSubview(self.timePickerFrom)

self.timePickerFrom.addTarget(self, action: #selector(self.dateChangedFrom(_:)), for: .valueChanged)

}
}

这是监听器函数:

func dateChangedFrom(_ sender: Any) {

let date = self.timePickerFrom.date
print(date)
let components = Calendar.current.dateComponents([.hour, .minute], from: date)
let hour = components.hour!
let minute = components.minute!
self.timeOfloadingLable.text = "\(hour):\(minute)"

}

所以问题是当我更改时间时,无论小时或分钟。时间向前推进 1 小时。

在监听器函数中,我从 timePicker 获取日期,当我打印它时,它是错误的,尽管我已经在上面设置了正确的日期

更新:

self.minDate=self.crrnt.getComperehensiveDate()

这是我使用的函数:

func setComperehensiveDate(longDate:Double){

let dbl = TimeInterval(longDate)

let date = Date(timeIntervalSince1970: dbl / 1000)


resultDate = date

}

func getComperehensiveDate()->Date{

print("bbbbbb",resultDate!)
return resultDate!
}

当时间为 11:59 时,会显示 11:33,如下所示

enter image description here

查看此视频:link

最佳答案

我刚刚检查了您的代码,运行良好,请确保您的 longDate 正确。

您传递了错误的纪元时间。在这里检查您的纪元时间:https://www.epochconverter.com/

关于swift - timePicker 的 Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51655707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com