当我说不能在后台运行,我的意思是,当用户双击了他们的家按钮,刷卡时,该按钮将重新启用如何禁用一个按钮,即使它在swift中不在后台运行?
这是我viewDidLoad
if let waitingDate:Date = UserDefaults.standard.value(forKey: "waitingDate") as? Date
{
let currentDate = Date()
if(currentDate.compare(waitingDate) == ComparisonResult.orderedDescending)
{
getPointsOutlet.isEnabled = true
}
}
代码这是代码在@IBAction func
getPointsOutlet.isEnabled = false
let newDate = Calendar.current.date(byAdding: .day, value: 1, to: Date())
UserDefaults.standard.setValue(newDate, forKey: "waitingDate")
尝试DATEVALUE作为!日期 – Phyber
并启用按钮检查:https://stackoverflow.com/questions/28571194/using-nsuserdefaults-to-add-a-24hour-countdown-timer-in-swift – Phyber
试试这个:为设置UserDefaults.standard .set(Date(),forKey:“yourKey”)....用于获取UserDefaults.standard.object(forKey:“yourKey”)as!日期 – KKRocks