gpt4 book ai didi

ios - 通知每分钟触发 6 次(swift4)

转载 作者:行者123 更新时间:2023-11-29 00:05:47 25 4
gpt4 key购买 nike

每次我的通知计时器关闭时,它都会在一分钟内重复 6 次。我只希望通知每分钟触发一次。我该如何编写代码,使通知每分钟只发出一次。

import UIKit
class ViewController: UIViewController {
var timer = Timer()
var passingDate : Date?
@IBOutlet var dptext: UITextField!
let datePicker = UIDatePicker()

override func viewDidLoad() {
createDatePicker()
timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(ViewController.testDate), userInfo: nil, repeats: true)
}

func createDatePicker() {
datePicker.datePickerMode = .dateAndTime
let toolbar = UIToolbar()
toolbar.sizeToFit()

let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
toolbar.setItems([doneButton], animated: false)

dptext.inputAccessoryView = toolbar
dptext.inputView = datePicker
}

@objc func testDate() {
if Calendar.current.isDate(datePicker.date, equalTo: Date(), toGranularity: .minute) {
print("success")
if let passingDate = passingDate, Calendar.current.isDate(datePicker.date, equalTo: passingDate, toGranularity: .minute) {
// Previous date existing, and is in the same minute as the current date : do nothing
return
}
passingDate = datePicker.date
print("success")
}
}

@objc func donePressed() {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .short
dptext.text = dateFormatter.string(from: datePicker.date)
self.view.endEditing(true)
}
}

最佳答案

timeInterval10 更改为 60 :

timer  = Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(ViewController.testDate), userInfo: nil, repeats: true)

关于ios - 通知每分钟触发 6 次(swift4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48084253/

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