Perform Segue from another class with helper function

I have two classes, ChargeCustomer and TwelvethViewController.
In TwelvethViewController I have a func goNextView() which should perform a segue to next view controller when called.
goNextView() is called from class ChargeCustomer inside a Alamofire completion handler.
The problem is that this function goNextView() is never called and I can’t figure out why.
I tried to make it work based on this answer on stackoverflow Perform Segue From Another Swift File via a class function

 class TwelvethViewController:UIViewController {

  //when this func is called, it should segue to Thiretheen
  func goNextView() {
    performSegue(withIdentifier: "twelvethToThiretheen", sender: self)
      }
 }

 class ChargeCustomer {

    //send a post request to the server
    Alamofire.request(urlString, method: .post, parameters: params, encoding: JSONEncoding.default, headers: ["Content-Type":"application/json"]).responseJSON {
        (response: DataResponse<Any>) in
                print("received reply from ALamofire") //prints string

           //perform segue to Thireteen View Controller
        func showNextView(fromViewController:TwelvethViewController) {
              print("went through")      //doesn't print
                 fromViewController.goNextView()
                    print("it has segued")      //doesn't print
           }      
      } ///end of Alamofire
   } //end of chargeUsingCustomer

Updated answer according to José Neto

  • SKAction runAction does not execute completion block
  • Swift UISearchController wired up in Core Data Project, app runs, but search not updating
  • Detect when user stopped / paused typing in Swift
  • Swift, Constraint, The view hierarchy is not prepared for the constraint
  • Switching language in running app for view elements in storyboard
  • Swift weakSelf in closure syntax
  •  class ChargeCustomer {
     //create instance of the viewcontroller that has segue with identifier    twelvethToThiretheen
       let twelvethVC = TwelvethViewController()
    
        //send a post request to the server
        Alamofire.request(urlString, method: .post, parameters: params, encoding: JSONEncoding.default, headers: ["Content-Type":"application/json"]).responseJSON {
            (response: DataResponse<Any>) in
                    print("received reply from ALamofire") //prints string
    
    //perform segue to Thirteen View Controller
       //crashes: <CleaningApp.TwelvethViewController: 0x7fd4e16ad2a0>) has no 
         //segue with identifier 'twelvethToThiretheen''
     self.showNextView(fromViewController: self.twelvethVC )
          } //end of Alamofire
    
        //perform segue to Thireteen View Controller
       func showNextView(fromViewController:TwelvethViewController) {
            fromViewController.goNextView()      
           }      
    } //end of chargeUsingCustomer
    

    Solutions Collect From Internet About “Perform Segue from another class with helper function”

    You have to instantiated the storyBoard that you want then the TwelvethViewController and finally you can push.
    Like this:

    let storyBoard = UIStoryboard(name: "youStoryBoard", bundle: nil) 
    let vc = storyboard?.instantiateViewController(withIdentifier: "youVCIndentifier") as! TwelvethViewController 
    self.navigationController?.pushViewController(vc, animated: true)