Search code examples
iosswiftxcodeseguepopover

Can't pass datas back from UIView Popover Dismiss


I have set a "Show as popover" segue between a UIView (A) and another UIView (B) (embed in a Navigation Controller) activated on a button's clic.

i am trying to pass datas back from (B) to (A) when i dismiss it (i want to keep the popover animation on both ways).

I have tried many methods i found mostly here, on Stackoverflow, but as of now i never successfully retrieved my data on (A).

I tried Delegates and protocols as well as other simpler methods. The last in date is the following one:

  • In (A), i just try to print the variable that should be storing the datas in ViewWillAppear :
class SearchBarsController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

    var testValue:String = ""

    override func viewWillAppear(_ animated: Bool) {
    print(testValue) // print is empty
    super.viewWillAppear(animated)
    }

}
  • In (B), i dismiss the popover and try to send the datas back on a button clic :
class SearchFilterViewController: UIViewController {

    @IBAction func DismissPopoverOnClic(_ sender: Any) {
            if let navController = presentingViewController as? UINavigationController {
                let presenter = navController.topViewController as! SearchBarsController
                presenter.testValue = "Test"
                print("success") //never called
            }
            self.dismiss(animated: true, completion: nil)
    }

}

on (B) i'd like to set up some filter that i'd use on (A) to present search results in a tableview. But actually the testValue's value is always blank.


Solution

  • oky so you can do it using unwind segue here is sample project :

    sample projecct

    process :

    Add this method to SearchBarsController below viewWillAppear

    @IBAction func unWindFromFilterViewController(_ sender: UIStoryboardSegue) {
    
        }
    

    Than go to Storyboard and go to SearchFilterViewController and then cntrl + Drag from DismissPopoverOnClic to top of the exit button then select unWindFromFilterViewController .

    Drag button to this exit and select unWindFromFilterViewController

    Than this the SearchFilterViewController write this method for passing data

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if let destVC = segue.destination as? ViewController {
               destVC.testValue = "Test"
    
            }
        }
    

    You will get your desired data back . thanks