我有两个班级,ChargeCustomer
和TwelvethViewController
。
在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
答案 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)