Search code examples
swiftuiviewcontrollerpopover

Swift, Pass data back from popover to view controller


I have sorted the original array in popover controller. Now I want to send that array back to the original view controller for tableview and map view.

Below is my code

 If propertyNameSrt == false
    {
        if ascSorting == false
        {
            properties.sort(sorterForbuildingAsc)
        }
        else
        {
            properties.sort(sorterForbuildingDesc)
        }


    }

My array is properties which includes custom object. How can pass this to my original view controller? Thanks in advance, Dhaval.


Solution

  • You can use delegate(protocol) methods to send back data to previous view controller.

    IN CURRENT VC:

    protocol MyProtocol: class
    {
        func sendArrayToPreviousVC(myArray:[AnyObject])
    }
    

    Make a var in your class.

    weak var mDelegate:MyProtocol?
    

    Now call the protocol method when you pop the view controller, with your "properties" array as parameter.

    mDelegate?.sendArrayToPreviousVC(properties)
    

    IN PREVIOUS VC:

    In your previous VC, set the mDelegate property to self, when you push the current VC.

    currentVC.mDelegate = self
    //PUSH VC
    

    Now implement the protocol method in your previous VC.

    func sendArrayToPreviousVC(myArray:[AnyObject]) {
        //DO YOUR THING
    }