How is it possible to get the handler of a UIAlertAction in Swift. It is set when initializing however I haven't found any property to get hold on the closure of the action. The closure is of type (UIAlertAction) -> Void however I would like to get the content of the closure so that I have some closure like () -> Void. Is this possible? Thanks for your answers
I've created a subclass for this as followed:
/// An UIAlertAction which saves the handler. Can be used for unit testing the action callback.
final class UIExecutableAlertAction: UIAlertAction {
private var handler: ((UIAlertAction) -> Swift.Void)?
static func with(title: String?, style: UIAlertActionStyle, handler: ((UIAlertAction) -> Swift.Void)? = nil) -> UIExecutableAlertAction {
let action = UIExecutableAlertAction(title: title, style: style, handler: handler)
action.handler = handler
return action
}
func execute() {
handler?(self)
}
}
Which can be used like this:
let myAction = UIExecutableAlertAction.with(title: "title", style: .destructive, handler: { [weak self] _ in
// Do something
})
There is NO member/property exposed by the UIAlertAction class. However we can manage this by ourselves by subclassing UIAlertAction and have some member named, say, "actionHandler" to store that.
Related
I am new to this escape function in Swift, but I follow a tutorial and I use the following function below: (the function is working for me)
static func showThreeOptions(messageText: String, titleOne:String, titleTwo: String, actionOne: #escaping () -> (Void), actionTwo: #escaping () -> (), currentView: UIViewController ) {
// create the alert
let alert = UIAlertController(title: "Alerta", message: messageText, preferredStyle: UIAlertController.Style.alert)
// add the actions (buttons)
alert.addAction(UIAlertAction(title: titleOne, style: UIAlertAction.Style.default, handler: { (alert) in
actionOne()
} ))
alert.addAction(UIAlertAction(title: titleTwo, style: UIAlertAction.Style.default, handler: { (alert) in
actionTwo()
} ))
alert.addAction(UIAlertAction(title: "Cancelar", style: UIAlertAction.Style.destructive, handler: nil))
// show the alert
currentView.present(alert, animated: true, completion: nil)
}
Now, I want to change the actionTwo() to actionTwo(number:Int),
but I don't know how to change the signature actionTwo: #escaping () -> ()
How can I change the signature
actionTwo: #escaping () -> () to allow to be able to call actionTwo(number:Int) ?
-----UPDATE-----
I create the function
actionTwo(2) and it works. Thank you #RobNapier
But there is another problem now.
I call the function
AlertActions.showThreeOptions(
messageText: "Resenha Finalizada.",
titleOne: "Marcas/Fotos",
titleTwo: "Editar",
actionOne: self.someHandlerOne,
actionTwo: self.someHandlerTwo(2),
currentView: self
)
This is the functions
func someHandlerOne() {
print("test")
}
func someHandlerTwo(_ id:Int) {
print("test2")
}
Now I get the following error when I call someHandlerTwo(_ id:Int)
Cannot convert value of type '()' to expected argument type '(Int) -> ()'
How can I fix that error?
-----UPDATE 2-----
I find out how to use a escaping function now
func notImplemented(resDado_id: Int) -> () {
print(resDado_id)
}
Change #escaping () -> () to #escaping (Int) -> (). Instead of something that takes no parameters, you want something that takes one.
It's a little nicer to use Void for return values that are (), like (Int) -> Void, but it means the same thing.
My suggestion is a different approach:
Write an extension of UIViewController and use the UIAlertAction handler signature for actionOne and actionTwo.
This is still more versatile and the UIAlertAction handler closures don't escape
extension UIViewController {
func showThreeOptions(messageText: String, titleOne:String, titleTwo: String, actionOne: ((UIAlertAction) -> Void)? = nil, actionTwo: ((UIAlertAction) -> Void)? = nil) {
// create the alert
let alert = UIAlertController(title: "Alerta", message: messageText, preferredStyle: .alert)
// add the actions (buttons)
alert.addAction(UIAlertAction(title: titleOne, style: .default, handler: actionOne))
alert.addAction(UIAlertAction(title: titleTwo, style: .default, handler: actionTwo))
alert.addAction(UIAlertAction(title: "Cancelar", style: .destructive, handler: nil))
// show the alert
self.present(alert, animated: true, completion: nil)
}
}
The closures can even be declared as functions for example
func actionOne(action : UIAlertAction) {
//
}
Edit:
You don't need to pass a parameter, you can create the handler inline and capture the id
func deleteSomething(at id: Int) {
let handler : (UIAlertAction) -> Void = { action in
db.deletarResDados(id: id)
}
showThreeOptions(messageText: "Resenha Finalizada.",
titleOne: "Marcas/Fotos",
titleTwo: "Editar",
actionOne: nil,
actionTwo: handler)
}
Basically I am currently adding functionX to everywhere i present an UIAlertController as seen below:
let alert = UIAlertController(title: "", message: "", preferredStyle: .alert)
let okAction = UIAlertAction(title: "ok", style: .default)
alert.addAction(okAction)
functionX(actionSheet: alert, controller: self)
self.present(alert, animated: true, completion: nil)
// or it can be
// tableviewController.present(alert, animated: true, completion: nil)
Instead of calling functionX every time, I want to override a present method and call functionX there. I attempted the following:
extension UIViewController {
override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
if (viewControllerToPresent is UIAlertController) {
functionX(actionSheet: viewControllerToPresent, controller: /* what should this be? */ )
}
super.present() //error here
}
}
Is this an appropriate approach? Can you help me fill the missing parameters?
i.e.:
What should be the controller? What would self or tableviewController from the first code stub be in the overriding present function?
How should I call the present method in the overriding present function?
According to the Swift guide,
Extensions can add new functionality to a type, but they cannot override existing functionality.
So you shouldn't really be overriding an existing method in a UIViewController in an extension.
What you could do is, to add your own present, called functionXAndPresent:
extension UIViewController {
func functionXAndPresent(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
if (viewControllerToPresent is UIAlertController) {
// to answer your second question, you should use "self" here
functionX(actionSheet: viewControllerToPresent, controller: self)
}
present(viewControllerToPresent, animated: flag, completion: completion)
}
}
You can't do this by overriding because as you have found out, you can't really refer to the "non-overridden" method at the end. super.present doesn't work because you are in an extension, not a subclass.
You can simply create a common method showAlert(with:and:) in a UIViewController extension and call functionX when the alert is presented, i.e.
extension UIViewController {
func showAlert(with title: String?, message: String?) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default)
alert.addAction(okAction)
self.present(alert, animated: true, completion: {
self.functionX(actionSheet: alert, controller: self)
})
}
func functionX(actionSheet: UIAlertController, controller: UIViewController) {
//your code here...
}
}
Usage:
Call the showAlert(with:and:) method from whatever controller you want to, be it a UIViewController or a UITableViewController or any other, i.e
self.showAlert(with: "Alery..!!!", message: "This is a sample alert.")
Overrides of NSObject's derivatives in Swift's static extensions is only for Objective-C compatibility. You cannot override in extensions of pure Swift declarations. Think of it in such manner that if the class itself adds an override, and then a static extension adds an override. Which implementation should the linker link? And which implementation does super call refer to? In Objective-C this behavior is undefined, in Swift the extension override is ignored all together.
What you could do instead is move the overriding function from the extension directly to the class.
I would like to create an action sheet that can be used several time in my code. To do so, I need to be able to use functions according to the action sheet title. Is there a way to pass functions as a parameter array like the "title" parameter?
//MARK: - UIAlert action sheet title
enum ActionSheetLabel: String {
case camera = "Camera"
case photoLibrary = "Album"
case cancel = "Cancel"
}
class CameraHandler {
static let cameraHandler = CameraHandler()
func openCamera() { }
func openPhotoLibrary() { }
}
//MARK: - Alert that shows an action sheet with cancel
extension UIViewController {
func showActionSheetWithCancel(vc: UIViewController, title: [ActionSheetLabel] /*Make a function parameter here to match title*/) {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
for value in title {
actionSheet.addAction(UIAlertAction(title: value.rawValue, style: .default, handler: {
(alert: UIAlertAction!) -> Void in
//Use the parameter function here to match title
}))
}
actionSheet.addAction(UIAlertAction(title: ActionSheetLabel.cancel.rawValue, style: .cancel, handler: nil))
vc.present(actionSheet, animated: true, completion: nil)
}
}
For UIAlert you just need to change preferredStyle .alert it and it's working for UIAlert And and below code just copy and paste it working for UIActionSheet.
extension UIViewController {
func popupAlert(title: String?, message: String?, actionTitles:[String?], actionStyle:[UIAlertAction.Style], actions:[((UIAlertAction) -> Void)?]) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
for (index, title) in actionTitles.enumerated() {
let action = UIAlertAction(title: title, style: actionStyle[index], handler: actions[index])
alert.addAction(action)
}
self.present(alert, animated: true, completion: nil)
}
}
Check below code For Usage
self.popupAlert(title: "Alert"), message: “Error in Loading”, actionTitles: ["Okey", "Email"], actionStyle: [.default, .default], actions: [nil,{ action in
// I have set nil for first button click
// do your code for second button click
}])
if you have any query then please comment me. Thank You
I have find out the best way to add an action sheet with cancel and as much action as needed.
Create an UIViewController extension with type alias:
//MARK: - Alert that shows an action sheet with cancel
extension UIViewController {
typealias AlertAction = () -> ()
typealias AlertButtonAction = (ActionSheetLabel, AlertAction)
func showActionSheetWithCancel(titleAndAction: [AlertButtonAction]) {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
for value in titleAndAction {
actionSheet.addAction(UIAlertAction(title: value.0.rawValue, style: .default, handler: {
(alert: UIAlertAction!) -> Void in
value.1()
}))
}
actionSheet.addAction(UIAlertAction(title: ActionSheetLabel.cancel.rawValue, style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
}
Then, in the class or other place where you want to use it, add the method this way:
//MARK: - UIAlert action sheet title
enum ActionSheetLabel: String {
case camera = "Camera"
case photoLibrary = "Album"
case cancel = "Cancel"
}
//MARK: - Class example where to use the action sheet action
class CameraHandler {
fileprivate let currentVC: UIViewController!
func openCamera() {
// Open user camera
}
func openPhotoLibrary() {
// Open user photo library
}
// Method example of this action sheet
func showActionSheetWithCameraAndLibrary(vc: UIViewController) {
//This is the way to use the extension
vc.showActionSheetWithCancel(titleAndAction: [
(ActionSheetLabel.camera, { [weak self] in self?.openCamera() }),
(ActionSheetLabel.photoLibrary, { [weak self] in self?.openPhotoLibrary() })
])
}
}
You can pass a closure and call it in the handler something like this should work.
Also not sure why you were passing the UIViewController , as you're already defining the function in a extension UIViewController therefore i allowed my self to remove it and used self.present instead .
extension UIViewController {
func showActionSheetWithCancel(title: [ActionSheetLabel], action: #escaping () -> ()?) {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
for value in title {
actionSheet.addAction(UIAlertAction(title: value.rawValue, style: .default, handler: {
(alert: UIAlertAction!) -> Void in
// action
action()
}))
}
let alertAction = UIAlertAction(title: ActionSheetLabel.cancel.rawValue, style: .cancel) { (_) in
action() // or for cancel call it here
}
actionSheet.addAction(alertAction)
self.present(actionSheet, animated: true, completion: nil)
}
}
As you can see #escaping () -> ()? is optional so you can pass nil too .
from what I understood you need to call a specific functions when the title of the alert changes & also you want to be able to do so from different viewControllers,
I hope this will help
extension UIViewController {
func showActionSheetWithCancel(vc: UIViewController, title: [ActionSheetLabel] ) {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let cameraHandler = CameraHandler()
for value in title {
switch value.rawValue {
case ActionSheetLabel.camera.rawValue:
actionSheet.addAction(UIAlertAction(title: ActionSheetLabel.camera.rawValue, style: .default, handler: { (alert) in
cameraHandler.openCamera()
}))
case ActionSheetLabel.photoLibrary.rawValue:
actionSheet.addAction(UIAlertAction(title: ActionSheetLabel.photoLibrary.rawValue, style: .default, handler: { (alert) in
cameraHandler.openPhotoLibrary()
}))
default:
actionSheet.addAction(UIAlertAction(title: ActionSheetLabel.cancel.rawValue, style: .cancel, handler: nil))
}
vc.present(actionSheet, animated: true, completion: nil)
}
}
}
and the call of the function will be like this:
showActionSheetWithCancel(vc: self, title: [UIViewController.ActionSheetLabel.camera])
Hello i am trying to create a kickass function to show alerts and run it's function. Buuut unfortunately Xcode and i am getting confused in here:
buttonAction:Array<(Any) -> Any)>
Expected '>' to complete generic argument list
func callAlert(_ view: UIViewController, title:String, message:String, buttonName:Array<String>, buttonAction:Array<(Any) -> Any)>) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
for index in 0..<buttonName.count{
alert.addAction(UIAlertAction(title: buttonName[index], style: .default, handler: { action in
switch action.style{
case .default:
print("default")
buttonAction()
case .cancel:
print("cancel")
case .destructive:
print("destructive")
}}))}
view.present(alert, animated: true, completion: nil)
}
How do i call function? Please check below:
callAlert(self,
title: "Donate type",
message: "Thanks for your support!",
buttonName: ["Buy me a coffee!","Something"]
)
First of all I highly recommend to implement the method as an extension of UIViewController.
Second of all I'd prefer presentAlert() over callAlert()
Third of all rather than two arrays for buttons and actions use one array of tuples for title, style and action.
By the way unspecified type (Any) -> Any is very, very bad because UIAlertAction handlers are clearly ((UIAlertAction) -> Void)?
Finally add an optional completion handler
extension UIViewController {
func presentAlert(title: String,
message: String,
alertActions: [(title: String, style: UIAlertAction.Style, action: ((UIAlertAction) -> Void)?)],
completion: (() -> Void)? = nil) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
for action in alertActions {
alert.addAction(UIAlertAction(title: action.title, style: action.style, handler: action.action))
}
self.present(alert, animated: true, completion: completion)
}
}
And use it inside an UIViewController
let buyCoffeeAction : (UIAlertAction) -> Void = { action in
// do something
}
let somethingAction : (UIAlertAction) -> Void = { action in
// do something
}
presentAlert(title: "Donate type",
message: "Thanks for your support!",
alertActions: [(title: "Buy me a coffee!", style: .default, action: buyCoffeeAction),
(title: "Something", style: .destructive, action: somethingAction)],
completion: nil)
Very often I have to show an alert to the user and I find myself writing the same code over and over again, so I built a convenience method.
When self.convenience.showAlertToUser() is called in viewDidLoad I get error for argument doThisAction cannot convert value of type Void to expected argument type () -> Void. I don't understand why, because I pass in an argument of that type. Also, I don't know if I am creating a retain cycle, so I would appreciate your help.
class ConvenienceMethods {
func showAlertToUser(alertMessage: String = "",actionOkTitle:String, actionCancelTitle:String, controller: UIViewController, cancelAction: Bool, doAction: #escaping (() -> Void)) {
let customAlert = UIAlertController(title: "", message: alertMessage, preferredStyle: .alert)
let actionCancel = UIAlertAction(title: actionCancelTitle, style: .cancel, handler: nil)
let actionOk = UIAlertAction(title: actionOkTitle, style: .default, handler: { (action: UIAlertAction) in
doAction()
})
if cancelAction == true {
customAlert.addAction(actionCancel)
}
customAlert.addAction(actionOk)
controller.present(customAlert, animated: true, completion: nil)
}
}
class ManageFeedbackTableViewController {
let convenience = ConvenienceMethods()
override func viewDidLoad() {
super.viewDidLoad()
let doThisAction = self.segueWith(id: "segueID")
self.convenience.showAlertToUser(alertMessage: "someMessage", actionOkTitle: "OK", actionCancelTitle: "No", controller: self, cancelAction: false, doAction: doThisAction)
}
//perform an action
func segueWith(id: String) -> Void{
self.performSegue(withIdentifier: id, sender: self)
}
}
Because you are to passing a reference to the function but the result itself.
Replace
let doThisAction = self.segueWith(id: "segueID")
By :
let doThisAction = { self.segueWith(id: "segueID") }
#bibscy,
doThisAction is a closure to which we can assign a block of code within "{ }" as follows:-
let doThisAction = { self.segueWith(id: "segueID") } which will work.