Disable/Reenable UIButton control

I am trying to prevent a button from being pressed in rapid succession. The app is designed for toddler's.
I am using am IBoutlet to which the button is connect and using button.enabled = false to disable the button and in viewDidApper i have the button.enabled = tru to re-enable the button. This actually executes but has no readl effect. I have attached a code snippet. Does anyone have a suggestion as to how this might be accomplished where the button does not become immediately available? Tried using sleep which actual achieved the goal but I believe this likely not the best solution.



@IBOutlet weak var btn1a: UIButton!

@IBAction func buttonPressed1(sender: UIButton) {

switch title {

case "btn1a":

var soundURL: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("01-S", ofType: "mp3")!)!

var error:NSError?

audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: &error)

audioPlayer.prepareToPlay()

audioPlayer.play()

/

btn1a.enabled = false

default:

break

} /

} /

override func viewDidLoad() {

super.viewDidLoad()

/

}

override func viewDidAppear(animated: Bool) {

super.viewDidAppear(animated)

/

btn1a.enabled = true

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

/

}

}


Galen Murrey

This actually executes

How have you checked this? Your code shown above contains some flaws and cannot be compiled.


but has no readl effect.

Describe what you expected and what had happened actually.

I didn't include all of the code. Yes, the complete code did execute, did exactly as expected. After the button was pressed, the code disabled the button as expeceted, played the audio clip then the button was re-enabled. It worked. I was just looking for some suggesstions as to whether ther is a better way to handle this where the user can not immediately press the same button again. I would prefer not to have to get into asynchronous


Thanks!

I may still be missing your intention, but as AVAudioPlayer plays the sound asynchronously, you cannot avoid asynchronous something.


If you intend to disable the button while the sound is playing, I would write something like this.

class MyViewController: UIViewController, AVAudioPlayerDelegate {
    //...

    @IBAction func buttonPressed1(sender: UIButton) {
        var soundURL: NSURL = NSBundle.mainBundle().URLForResource("01-S", withExtension: "mp3")!
        var error:NSError?
        audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: &error)
        audioPlayer.delegate = self
        audioPlayer.prepareToPlay()
        audioPlayer.play()
        sender.enabled = false
    }
    func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool) {
        btn1a.enabled = true
    }

    //...
}

Thank You for your kind answer. I am new to ios development with much to learn. We all have to start somewhere. I will give this a try. Again, thank you. Everyone on the forum should be as kind and understanding...

Disable/Reenable UIButton control
 
 
Q