使用辅助函数从另一个类执行Segue

时间:2017-02-25 16:13:34

标签: swift segue

我有两个班级,ChargeCustomerTwelvethViewController。   在TwelvethViewController中,我有一个func goNextView(),它应该在调用时执行到下一个视图控制器的segue。 {A}在Alamofire完成处理程序中从goNextView()调用class ChargeCustomer。 问题是这个函数goNextView()从未被调用过,我无法弄清楚原因。 我尝试根据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

根据JoséNeto

更新了答案
 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

1 个答案:

答案 0 :(得分:2)

你必须实例化你想要的StoryBoard然后是TwelvethViewController,最后你可以推送。 像这样:

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