iOS禁用UIButton 30分钟

时间:2016-02-01 01:43:24

标签: ios uibutton

我正在创建一个应用程序,人们可以每小时最多请求两个订单。我想禁用"订单"一旦按下UIButton整整30分钟。任何人都可以建议最有效的方法来做到这一点但是完全阻止用户订购两次,即使应用程序被杀死了吗?感谢。

4 个答案:

答案 0 :(得分:4)

在高级别你需要做两件事:

  1. 计算再次启用该按钮时的NSDate,并将该日期存储在NSUserDefaults
  2. 开始在30分钟内完成的NSTimer。在计时器关闭时启用禁用按钮,并从NSUserDefaults
  3. 中删除日期

    应用程序很可能会进入后台,计时器将在30分钟之前停止很长时间。这意味着您的应用需要在进入后台时停止计时器。当它返回到前台时,您会查看NSUserDefaults中的日期并查看剩余时间。如果时间已过,请启用该按钮并从NSUserDefaults中删除日期。如果没有,请在所需的时间后启动另一个计时器,如上面的步骤2所示。

答案 1 :(得分:1)

您应该在启动的NSUserdefaults.Once应用中保存订单日期,检查最后一个订单日期并为此设置倒数计时器。

答案 2 :(得分:1)

这是我之前想到的问题的方法。您将使用的三件事是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()
}

答案 3 :(得分:0)

按下该按钮时,禁用该按钮并使用NSDate对象记录当前时间。为确保即使应用程序被杀,它仍然存在,请确保编写它 - 如果您的应用程序尚未使用数据系统,NSUserDefaults可能是解决此问题的最简单方法。

接下来,您需要为按钮创建一种机制,以便再次启用。最简单可靠的方法是创建一个NSTimer来检查记录的日期是否超过30分钟,如果是,则启用该按钮。

以下是Swift中如何执行此操作的示例:

class ViewController: UIViewController {
    @IBOutlet weak var btn: UIButton!
    var enableTimer: NSTimer!
    let defaults = NSUserDefaults.standardUserDefaults()

    //Because this is a computed property, it auto-saves and persists
    var lastPushed: NSDate {
        get {
            if let unwrappedDate = defaults.objectForKey("lastPushed") as? NSDate {
                return unwrappedDate
            } else {    //If date not yet set
                return NSDate(timeIntervalSince1970: 0)
            }
        } set {    //NSDate is already compatible with NSUserDefaults
            defaults.setObject(newValue, forKey: "lastPushed")
        }
    }

    override func viewDidLoad() {
        startTimer()
    }
    func startTimer() {
        enableTimer = NSTimer.scheduledTimerWithTimeInterval(30, self, Selector("enableTim:"), nil, true)
    }
    @IBAction btnPressed() {
        lastPushed = NSDate()     //NSDate with current time
        startTimer()
        btn.enabled = false
    }
    func enableTim(timer: NSTimer) {
        if (lastPushed.timeIntervalSinceNow < -1800) {    //If last pressed more than 30 minutes ago
            btn.enabled = true
            enableTimer.stop()
        }
    }
}