Last active
May 8, 2019 10:17
-
-
Save joshua24322/2b48a026452e1184fce85ef68f18014e to your computer and use it in GitHub Desktop.
This's sample extension for UIAlertController to show customized message and fixable use the completion handler, useful to any UIViewController Class
This file contains hidden or 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
/** | |
This is flexible to deal the completion handler. | |
* gist: https://gist.github.com/joshua24322/2b48a026452e1184fce85ef68f18014e | |
*/ | |
import Foundation | |
import UIKit | |
extension UIViewController { | |
/** | |
* may you wanna pure message alert: | |
### Usage Example: ### | |
```` | |
callbackAlert(title: "message") | |
callbackAlert(title: "message", message: "message") | |
```` | |
* or you wanna the alert message do something in completion handler: | |
(the second parameter which message could be nil) | |
### Usage Example: ### | |
```` | |
callbackAlert(title: "message", message: nil, okCompletion: { | |
// code here | |
}) { | |
// code here | |
} | |
```` | |
*/ | |
func callbackAlert(title: String, message: String? = nil, okCompletion: @escaping (() -> ()) = {}, presentCompletion: @escaping (() -> ()) = {}) { | |
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) | |
let okAction = UIAlertAction(title: "OK", style: .default) { (action: UIAlertAction) in | |
okCompletion() | |
} | |
alertController.addAction(okAction) | |
DispatchQueue.main.async { | |
self.present(alertController, animated: true) { | |
presentCompletion() | |
} | |
} | |
} | |
/** | |
* may you wanna pure message dialog: | |
### Usage Example: ### | |
```` | |
callbackDialog(title: "message") | |
callbackDialog(title: "message", message: "message") | |
```` | |
* you can ignore any one completion handler by @escaping (() -> ()) = {} | |
(ignore the okCompletion and the presentCompletion) | |
### Usage Example: ### | |
```` | |
callbackDialog(title: "message", message: "message", cancelCompletion: { | |
// code here | |
}) | |
```` | |
*/ | |
func callbackDialog(title: String, message: String? = nil, okCompletion: @escaping (() -> ()) = {}, cancelCompletion: @escaping (() -> ()) = {}, presentCompletion: @escaping (() -> ()) = {}) { | |
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) | |
let okAction = UIAlertAction(title: "OK", style: .default) { (action: UIAlertAction) in | |
okCompletion() | |
} | |
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (cancel: UIAlertAction) in | |
cancelCompletion() | |
} | |
alertController.addAction(okAction) | |
alertController.addAction(cancelAction) | |
DispatchQueue.main.async { | |
self.present(alertController, animated: true) { | |
presentCompletion() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use Markdown mark