Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 442
dhaval shah Avatar asked Jun 02 '15 12:06

dhaval shah


1 Answers

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
}
like image 130
sudhanshu-shishodia Avatar answered Jan 09 '23 08:01

sudhanshu-shishodia