Last active
January 18, 2017 06:57
-
-
Save evgeniyd/c534c028d6b4478800dcacd06a382051 to your computer and use it in GitHub Desktop.
Swift 3: @autoclosure syntax to call functions upon UIAlertViewController's callbacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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