Skip to content

Instantly share code, notes, and snippets.

@evgeniyd
Last active January 18, 2017 06:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evgeniyd/c534c028d6b4478800dcacd06a382051 to your computer and use it in GitHub Desktop.
Save evgeniyd/c534c028d6b4478800dcacd06a382051 to your computer and use it in GitHub Desktop.
Swift 3: @autoclosure syntax to call functions upon UIAlertViewController's callbacks
/// The Medium artcile, discussing the approach
/// https://medium.com/@euginedubinin/swift-useful-autoclosure-when-presenting-uialertviewcontroller-b592d1643a50#.kx0fuqm1x
final class NoteViewController: UIViewController {
// ... set up target-action for an Edit UIButton somewhere here ...
private dynamic func handleEditButton() {
presentEditConfirmationDialog(onEdit: self.editArticle(),
onCancel: () ) /* assuming you have nothing to do upon cancellation */
}
private func presentEditConfirmationDialog(onEdit: @escaping @autoclosure (Void)->Void, onCancel: @escaping @autoclosure (Void)->Void ) {
let message = "Do you want to edit the note?"
let actionSheetController: UIAlertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel",
style: .cancel) { _ in
onCancel()
}
actionSheetController.addAction(cancelAction)
let editAction: UIAlertAction = UIAlertAction(title: "Edit",
style: .default) { _ in
onEdit()
}
actionSheetController.addAction(editAction)
self.present(actionSheetController, animated: true, completion: nil)
}
// ... other code ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment