即使按钮不在后台快速运行,如何禁用按钮? [英] How to disable a button even when it is not running in the background in swift?
本文介绍了即使按钮不在后台快速运行,如何禁用按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我说不在后台运行时,我的意思是当用户双击他们的主页按钮并向上滑动时,该按钮将重新启用
When I say not running in the background, I mean when the user double clicked their home button and swipes up, the button will reenable
这是我的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")
推荐答案
要获取值,请始终检查使用 guard 或 if let 语句以防止崩溃:-
To get the value always check use guard or if let statement to prevent the crash:-
//Set value in user default
UserDefaults.standard.set(Date(), forKey:"date")
UserDefaults.standard.synchronize()
//Get value from user default
guard let value = UserDefaults.standard.object(forKey: "date") as? Date else {return}
print(value)
这篇关于即使按钮不在后台快速运行,如何禁用按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文