UIAlertAction

In order to display alerts, I made a simple class:

Code Block
public protocol AlertDisplayer {
    func displayAlert(with title: String, message: String, actions: [UIAlertAction]?, completion: (() -> Void)?)
}
extension AlertDisplayer where Self: UIViewController {
    public func displayAlert(with title: String, message: String, actions: [UIAlertAction]? = nil, completion: (() -> Void)? = nil ) {
        guard presentedViewController == nil else {
            return
        }
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        actions?.forEach { action in
            alertController.addAction(action)
        }
        present(alertController, animated: true, completion: nil) //{
    }
}


Now I make a class to this class, from another class, like this:

Code Block
func passwordForgot(navigation: UINavigationController, email: String?) {
        let model = loginModel()
        model.rappelpwd(controller: navigation.topViewController as! AlertDisplayer, email: email!) {
            result in
            if result.result! {
                let defaultAction = UIAlertAction(title: "OK", style: .default, handler: {
                    action  in
                    Swift.print("ok tapped")
                    //presentControllerLogin()
                })
                let title = "Attention"
                let aController = navigation.topViewController as? AlertDisplayer
                if aController != nil {
                    aController!.displayAlert(with: title , message: result.msg!, actions: [defaultAction], completion: nil)
                }
            }
        }
    }

Everyting is OK, but when I tap the OK button, the handler is never called




Answered by PatriceRapaport in 648756022
I'm very ashamed
It effectively was an internal error.
The code was never called

Please excuse me, all of you, for disturbing

when I tap the OK button, the handler is never called

I created a brand-new project, copied your code exactly as is, and tested.
When I tap the OK button in the alert shown, ok tapped is shown in the debug console.

So, if something is going wrong, something in the hidden parts of your code may be affecting.
Please show enough info to reproduce your issue.


One more, AlertDisplayer is a protocol, not a class.
It is pretty risky to force unwrap the email
Code Block
        model.rappelpwd(controller: navigation.topViewController as! AlertDisplayer, email: email!)

you'd better use nil coalescing
Code Block
        model.rappelpwd(controller: navigation.topViewController as! AlertDisplayer, email: (email ?? "")) {

Have you checked that aController is not nil ? That should occur if your topViewController does not conform to the protocol.
Instrument code like this:
Code Block
if aController != nil {
aController!.displayAlert(with: title , message: result.msg!, actions: [defaultAction], completion: nil)
} else {
print("Controller is nil")
}


But that should not, otherwise force unwrap should crash line 3
I confirm the variable aController is not nil so displayAlert is called

Accepted Answer
I'm very ashamed
It effectively was an internal error.
The code was never called

Please excuse me, all of you, for disturbing

Please excuse me, all of you, for disturbing

Please do not mind. Everyone can make mistakes, and every mistakes can be recovered.
May your app be great!
UIAlertAction
 
 
Q