Skip to content

Instantly share code, notes, and snippets.

@MochaTheCoder
Last active October 4, 2019 03:19
Show Gist options
  • Save MochaTheCoder/e798218567fa98ee0398365d235772e5 to your computer and use it in GitHub Desktop.
Save MochaTheCoder/e798218567fa98ee0398365d235772e5 to your computer and use it in GitHub Desktop.
class VC: UIViewController {
private let asyncButton = UIButton()
func doAsyncAction() {
// Change button to display loading when we click on it
asyncButton.isEnabled = false
asyncButton.setTitle("Loading", for: .disabled)
asyncButton.setTitleColor(.red, for: .disabled)
networkManager.makeRequest() { response in
// Do network stuff
// Sets button title text and color back to original for .normal
// Notice we don't need to call setTitle / setTitleColor again
DispatchQue.main.async {
self.asyncButton.isEnabled = true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment