简体   繁体   中英

Swift 4 – Custom Alerts with protocols

I'm having issues with Custom alerts and sending actions back to VC from which alert was called.

I have two classes:

  • Factory
  • ConfirmationAllert

User journey I'm trying to achieve:

The user performs actions in the Factory class after he finishes I call ConfirmationAllert using such code:

func showAlert() {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let myAlert = storyboard.instantiateViewController(withIdentifier: "ConfirmationAllert")
    myAlert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
    myAlert.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
    (view as? UIViewController)?.present(myAlert, animated: true, completion: nil)
}

In ConfirmationAllert class I have button, which:

  • dismisses alert
  • sends action to Factory - this action is to dismiss Factory VC and go back to previous VC.

First action completes successfully, the second action not working. I'm using protocols to send the second action to Factory VC, but something is not working, and I don't know what.

Here is my code:

Factory

final class FactoryViewController: UIViewController {
    let alert = ConfirmationAllert()

    @IBAction func didPressSave(_ sender: UIButton) {
        showAlert() 
    }

    func goToPreviousVc() {
        alert.delegate = self
        print("Inside factory") -> print don't get called
        // navigationController?.popViewController(animated: true) -> none of this works
        // dismiss(animated: true, completion: nil) -> none of this works
    }
}

extension FactoryViewController: ConfirmationAllertDelegate {
    func dismissVC() {
        goToPreviousVc()
        print("Go to previous")
    }
}

ConfirmationAllert

protocol ConfirmationAllertDelegate {
    func dismissVC()
}

class ConfirmationAllert: UIViewController {
    var delegate: ConfirmationAllertDelegate?

    @IBAction func didPressOk(_ sender: UIButton) { 
        self.delegate?.dismissVC() 
    }
}

I didn't include viewDidLoad methods as I'm not calling anything there.

My issue is that method goToPreviousVc() doesn't perform any actions.

Thank you in advance for your help!

I guess your problem is that you setup your ConfirmationAllertDelegate at goToPreviousVc that supposed to be called using that delegate.

Instead, try to set up you delegate when you creating myAlert object

let myAlert = storyboard.instantiateViewController(withIdentifier: "ConfirmationAllert")
(myAlert as? ConfirmationAllert).delegate = self
// the rest of your code

After that, your alert will have a delegate since it was created and when you press the button, it should work as you expect.

Try to use below code

func showAlert() {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let myAlert = storyboard.instantiateViewController(withIdentifier: "ConfirmationAllert") as! ConfirmationAllert
    myAlert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
    myAlert.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
    myAlert.delegate = self
    present(myAlert, animated: true, completion: nil)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM