简体   繁体   English

从另一个类错误执行segue找不到segue

[英]Perform segue from another class error can't find segue

I am trying to perform a segue that will be call form another class than ui view. 我正在尝试执行将由ui视图之外的另一个类调用的segue。 The final target is that the main view will wait that an URL request is done to go to the next view. 最终目标是主视图将等待URL请求完成后再转到下一个视图。

Here my ui view: 这是我的ui视图:

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'' ***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'Receiver()没有标识为'segueToMainController的segue”

It doesn't make any sense to instantiate a new LoadingViewController and try to pass that into your class func. 实例化一个新的LoadingViewController并尝试将其传递到您的类func中没有任何意义。 What you need to do is get a reference to your existing LoadingViewController and call goToNextView() on that. 您需要做的是获取对现有LoadingViewController的引用,并在其上调用goToNextView()

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() . 有许多方法可以做到这一点,但是一种方法是将对您的LoadingViewController的引用作为变量传递给另一个视图,并且当异步调用完成时,您可以调用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: 您还可以使用NotificationCenter从视图中广播此异步调用已完成,并在LoadingViewController上观察该通知,并使用该事件触发segue:

In your LoadingViewController 's viewDidLoad method: 在您的LoadingViewControllerviewDidLoad方法中:

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:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM