it possible to Pass Data with popViewControllerAnimated?

前端 未结 5 1354
清歌不尽
清歌不尽 2020-12-06 02:29

I came across an interesting problem, i have main ViewController let\'s call him MainVC with navigationController and i am doing performSegueWithIdentifier from him to Mine

5条回答
  •  情歌与酒
    2020-12-06 02:35

    I simply set up a protocol in the view being dismissed (example in Swift):

     protocol ExampleTableViewControllerDismissDelegate {
        func didDismiss(withData: Any)
    }
    
    var delegate: SearchableTableViewControllerDismissDelegate?
    

    You can then call this when you dismiss/pop your view like this

    self.navigationController?.popViewController(animated: true)
    delegate?.didDismiss(withData: Any)
    

    Then in the view being dismissed to (the parent in the hierarchy), we can conform to the delegate and essentially get a callback with the data after the view has been dismissed.

    //MARK: ExampleTableViewControllerDismissDelegate
    
    func didDismiss(withData: Any) {
        //do some funky stuff
    }
    

    And don't forget to subscribe to the delegate in the parent view by using

    viewController.delegate = self
    

提交回复
热议问题