gpt4 book ai didi

ios - iOS禁用UIButton 30分钟

转载 作者:行者123 更新时间:2023-12-01 17:37:34 26 4
gpt4 key购买 nike

我正在制作一个应用程序,人们可以每小时最多请求两个订单。我想禁用“订单” UIButton一整整30分钟。有人可以建议这样做的最有效方法,但是即使该应用被终止,也可以完全阻止用户订购两次?谢谢。

最佳答案

这是我之前针对您的问题想到的方法。您将使用的三件事是NSDateNSTimeIntervalNSUserDefaults

// I threw this in Xcode to aide me in typing this solution.
// You probably dragged a button from storyboard...leave it like that.
let orderButton: UIButton?

// Put toggleOrderButtonAvailability() in viewDidLoad and viewDidAppear

func toggleOrderButtonAvailability() {
// get current date
let currentDate = NSDate()

// we're not sure if there's a value for this, but we're creating a variable for it
// it will nil if the user hasn't placed an order
var lastOrderDate: NSDate?

// we're creating a variable to check
var timeSinceLastOrder: NSTimeInterval?

// if a value for the lastOrderDate saved in NSUserDefaults, then...
if NSUserDefaults.standardUserDefaults().objectForKey("lastOrderDate") != nil {
lastOrderDate = NSUserDefaults.standardUserDefaults().valueForKey("lastOrderDate") as? NSDate
// calculate minutes since last order
// 1800 seconds = 60 seconds per minute X 30 minutes
timeSinceLastOrder = (currentDate.timeIntervalSinceDate(lastOrderDate!)) / 1800

if timeSinceLastOrder < 30 {
orderButton?.enabled = false
// Some alert to let the user know they can't order for another X minutes
// TODO: you could create a class variable like "timeUntilButtonReenabled"
// and set it here, then the timer would run and call this method when it's
// done to re-enable the button. Set the timer in viewDidAppear()

} else {
orderButton?.enabled = true
}
}
}

您还需要在下订单时设置 lastOrderDate,并且可以调用我们刚创建的方法来在下订单时禁用按钮。
@IBAction func orderButtonAction(sender: UIButton) {

// Whatever you do when you send an order

// Set the lastOrderDate & disable the button
let currentDate = NSDate()
NSUserDefaults.standardUserDefaults().setObject(currentDate, forKey: "lastOrderDate")
toggleOrderButtonAvailability()
}

关于ios - iOS禁用UIButton 30分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35121855/

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