Search code examples
swiftuialertcontroller

Alert button to dismiss Alert and go back to previous View Controller


I'm kind of new to Swift and I can't figure this out. I have an Alert that should show on a successful URL request. After a user clicks an Ok button on the alert, I need the alert dismissed and I need the presented controller to go back in the navigation stack to the previous view controller. I don't get any errors but nothing happens. If I move the entire code for the Alert inside the CustomClass, then it works fine. I assume I am not referencing the CustomClass the right way. Any help would be greatly appreciated!

 struct Alert {
    static func CustomAlert(vc: UIViewController, title: String, message: String){

        var title = "Title...!"
        var message = "Message..."
        let myAlert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        myAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (actin) in
            myAlert.dismiss(animated: true, completion: nil)
            let vc = CustomClass()
            vc.GoBackToPreviousVC()
        }))
         vc.present(myAlert, animated: true, completion: nil)
    }
 }

 class: CustomClass: UIViewController {

    func GoBackToPreviousVC(){
        navigationController?popViewController(animated: true)
    }

    function Download(){

      code for URLRequest...

      DispatchQueue.main.async {
        if (self.response.Status == "200"){
            Alert.CustomAlert(vc: self, title: "", message: "")
        }

      }

    }
}

Solution

  • Dont create new instance let vc = CustomClass() use the one you passed as parameter

    struct Alert {
       static func CustomAlert(vc: UIViewController, title: String, message: String){
    
           var title = "Title...!"
           var message = "Message..."
           let myAlert = UIAlertController(title: title, message: message, preferredStyle: .alert)
           myAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (actin) in
               myAlert.dismiss(animated: true, completion: nil)
    
            if let controller = vc as? CustomClass {
               controller.GoBackToPreviousVC()
            }
           }))
            vc.present(myAlert, animated: true, completion: nil)
       }
    }
    

    And better to use Protocol instead of hard code class

    protocol Goback {
        func GoBackToPreviousVC()
    }
    
    
    struct Alert {
       static func CustomAlert(vc: UIViewController, title: String, message: String){
    
           var title = "Title...!"
           var message = "Message..."
           let myAlert = UIAlertController(title: title, message: message, preferredStyle: .alert)
           myAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (actin) in
               myAlert.dismiss(animated: true, completion: nil)
    
            if let controller = vc as? Goback {
               controller.GoBackToPreviousVC()
            }
           }))
            vc.present(myAlert, animated: true, completion: nil)
       }
    }
    

    And confirm your class with that protocol in which you want to use Alert

    class CustomClass: UIViewController,Goback {
    
        func GoBackToPreviousVC(){
    
            navigationController?.popViewController(animated: true)
        }
    }