In Swift, you can also use defer
keyword, to execute a block of code that will be executed only when execution leaves the current scope.
@IBAction func btnAction(_ sender: UIButton) { sender.isUserInteractionEnabled = false defer { sender.isUserInteractionEnabled = true } // rest of your code goes here}
Note: This will only be helpful if the "rest of your code" is not async
, so that the execution actually leaves the current scope. In async cases you'd need to set isUserInteractionEnabled = true
at the end of that async method.