使用辅助函数从另一个类执行 Segue
Posted
技术标签:
【中文标题】使用辅助函数从另一个类执行 Segue【英文标题】:Perform Segue from another class with helper function 【发布时间】:2017-02-25 16:13:34 【问题描述】:我有两个班级,ChargeCustomer
和 TwelvethViewController
。
在 TwelvethViewController 中,我有一个 func goNextView()
,它应该在调用时执行到下一个视图控制器的 segue。
goNextView()
在 Alamofire 完成处理程序中从 class ChargeCustomer
调用。
问题是这个函数goNextView()
从来没有被调用过,我不知道为什么。
我试图根据***上的这个答案使其工作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
【问题讨论】:
您在 alamofire 闭包中声明 showNextView 函数,但您从未调用该函数。 @JoséNeto 我已更新,但应用程序崩溃并出现错误<CleaningApp.TwelvethViewController: 0x7fd4e16ad2a0>) has no segue with identifier 'twelvethToThiretheen''
。我已经检查了 sotryboard 中的 segue 标识符是否正确,并且是正确的。谢谢
尝试不使用 segue,实例化 TwelvethViewController 控制器并推送它。
试试这个 let storyBoard = UIStoryboard(name: "youStoryBoard", bundle: nil) let vc = storyboard?.instantiateViewController(withIdentifier: "youVCIndentifier") as! TwelvethViewController self.navigationController?.pushViewController(vc, animated: true)
@JoséNeto 它有效。非常感谢
【参考方案1】:
你必须实例化你想要的故事板,然后是 TwelvethViewController,最后你可以推送。 像这样:
let storyBoard = UIStoryboard(name: "youStoryBoard", bundle: nil)
let vc = storyboard?.instantiateViewController(withIdentifier: "youVCIndentifier") as! TwelvethViewController
self.navigationController?.pushViewController(vc, animated: true)
【讨论】:
以上是关于使用辅助函数从另一个类执行 Segue的主要内容,如果未能解决你的问题,请参考以下文章