3

The following is the code for an IBAction I would like to disable for 5 seconds after it is clicked.

@IBAction func postPressed(sender: AnyObject) {
        //var disableMyButton = sender as? UIButton
        //disableMyButton!.enabled = false
        //NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector:"enableButton", userInfo: nil, repeats: false)
    if(currLocation != nil){
        let testObject = PFObject(className: "Hey")
        testObject["text"] = self.postView.text
        testObject["count"] = 0
        testObject["replies"] = 0            
        testObject["location"] = PFGeoPoint(latitude: currLocation!.latitude , longitude: currLocation!.longitude)
        testObject["comments"] = []
        testObject["user"] = PFUser.currentUser()
        testObject.saveInBackgroundWithBlock({ (success, error) -> Void in
            if success {
                self.dismissViewControllerAnimated(true , completion: nil)
                if let completion = self.completion {
                    completion(self)
                }
            }
        })
    } else {
        alert()
    }  
}

The commented out code is my attempt at disabling the button for 5 seconds, but this code crashes, and I believe it crashed because there is no reference to the enable button.

Typically I would do something like

func enableButton() {
   self.button.enabled = true
}

But this won't work since it's an IBAction and theres no reference to the button besides there. Maybe there is a way to do so, but I'm at a loss.

Thanks in advance for some help.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
James Chen
  • 387
  • 1
  • 4
  • 17
  • 1
    I presume you want to do this because you're waiting for the async operation to complete. Five seconds is bad because it might be more or less than the actual time required to complete the operation. Since you already have a callback block, why not just disable the button before you call `saveInBackgroundWithBlock()` then reënable it inside the completion block if you decide not to dismiss the viewcontroller (which does destroy the button). – i_am_jorf Oct 01 '15 at 17:44
  • @i_am_jorf Well yeah sort of. I'm doing this because the user can tap the button a few times if the connection is slow and make multiple posts with the same content. – James Chen Oct 01 '15 at 18:02

4 Answers4

5

Try this,

  1. while tap the action set button disable.
  2. set some interval to enable your button by using delay function.

Ex:

let tapGesture = UITapGestureRecognizer(target: self, action: "tapaction")
tapGesture.numberOfTapsRequired = 1
Your_Button_name.addGestureRecognizer(tapGesture)


@IBAction func tapaction()
{
    Your_Button_name.disable=true
    //Delay function to enable your button
    NSTimer.scheduledTimerWithTimeInterval(Your_time_value_delay, target: self, selector: Selector("enablefunc"), userInfo: nil, repeats: false)
}

func enablefunc()
{
   Your_Button_name.disable=false
}
Manikandan D
  • 1,422
  • 1
  • 13
  • 25
2

SWIFT 5.2

within UIButton Action place this code:

yourButtonOutlet.isEnabled = false
                //Delay function to enable your button for 3 seconds
            Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.enablefunc), userInfo: nil, repeats: false)

Then outside of the UIButton func put:

@objc func enablefunc()
{
    yourButtonOutlet.isEnabled = true
}
David_2877
  • 257
  • 4
  • 7
1
   var disableMyButton = sender as? UIButton
   disableMyButton!.enabled = false
   testObject.saveInBackgroundWithBlock({ (success, error) -> Void in
        if success {
            self.dismissViewControllerAnimated(true , completion: nil)
            if let completion = self.completion {
                completion(self)
            }
        } else {
            disableMyButton!.enabled = true
        }
    }

Also, depending on what completion(self) does, you may want to do it before you dismiss the view controller. I dunno.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • If you succeed, the view controller (which contains the button, I assume) is dismissed. So why do you care to reënable it? I mean, you can do it right before `if success {` if you want. Assume I don't know anything about your code structure except what you posted. My psychic powers are stretched thin today. :) – i_am_jorf Oct 01 '15 at 17:53
  • Hm. everything crashes when I try this. – James Chen Oct 01 '15 at 18:02
1

you can use the dispatch framework to add a delay. Use the main thread.

disableMyButton!.enabled = false
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(5 * Double(NSEC_PER_SEC)))

dispatch_after(delay, dispatch_get_main_queue()) {
    disableMyButton!.enabled = true
}
Ishan Handa
  • 2,271
  • 20
  • 25