I am trying to perform a segue that will be call form another class than ui view. The final target is that the main view will wait that an URL request is done to go to the next view.
Here my ui view:
class LoadingViewController: UIViewController {
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
CardList.retrieveAllCards()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func goToNextView() {
performSegue(withIdentifier: "segueToMainController", sender: LoadingViewController.self)
}
class ShowNext {
class func view(fromViewController: LoadingViewController) {
fromViewController.goToNextView()
}
}
}
and here how I call my segue from the other class
let task = session.dataTask(with: request){
data,response,error in
do
{
if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary {
jsonResultEnd = jsonResult
//print("SUCCESS:\(jsonResult)")
LoadingViewController.ShowNext.view(fromViewController: LoadingViewController())
print("loading ended")
}
} catch let error as NSError {
print("ERROR request manager: \(error.localizedDescription)")
}
}
Here is the error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'segueToMainController''
It doesn't make any sense to instantiate a new LoadingViewController
and try to pass that into your class func. What you need to do is get a reference to your existing LoadingViewController
and call goToNextView()
on that.
There are many ways to do this, but one way would be to pass a reference to your LoadingViewController
as a variable on your other view, and when your async call finishes you can call self.loadingViewController.goToNextView()
.
You could also use NotificationCenter
to broadcast from your view that this async call is complete, and observe that notification on your LoadingViewController
and use that event to trigger the segue:
In your LoadingViewController
's viewDidLoad
method:
NotificationCenter.default.addObserver(self, selector: #selector(goToNextView), name: Notification.Name("loadingComplete"), object: nil)
And in your other view's async callback:
NotificationCenter.default.post(name: Notification.Name("loadingComplete"), object: