How to pass prepareForSegue: an object

后端 未结 10 2086
北荒
北荒 2020-11-21 11:18

I have many annotations in a mapview (with rightCalloutAccessory buttons). The button will perform a segue from this mapview to a tableview

10条回答
  •  旧时难觅i
    2020-11-21 11:40

    I used this solution so that I could keep the invocation of the segue and the data communication within the same function:

    private var segueCompletion : ((UIStoryboardSegue, Any?) -> Void)?
    
    func performSegue(withIdentifier identifier: String, sender: Any?, completion: @escaping (UIStoryboardSegue, Any?) -> Void) {
        self.segueCompletion = completion;
        self.performSegue(withIdentifier: identifier, sender: sender);
        self.segueCompletion = nil
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        self.segueCompletion?(segue, sender)
    }
    

    A use case would be something like:

    func showData(id : Int){
        someService.loadSomeData(id: id) {
            data in
            self.performSegue(withIdentifier: "showData", sender: self) {
                storyboard, sender in
                let dataView = storyboard.destination as! DataView
                dataView.data = data
            }
        }
    }
    

    This seems to work for me, however, I'm not 100% sure that the perform and prepare functions are always executed on the same thread.

提交回复
热议问题