Chris
Chris

Reputation: 249

Swift: Trying to return selection to previous view controller

This seems to be a common problem, but none of the many solutions I've tried have seemed to work (or I'm not executing them properly).

I've got an image on FirstImageVC, I push a button to bring up a new view collection view controller with some custom images, the user selects one, and I want to send that image back to the FirstImageVC to overlay the original image, sort of like a sticker.

I just can't get it to execute a segue of any kind on selecting an image. Here's what I'm sort of working with in the second view controller. And it seems I may need to be adding something to the original VC, too, no?

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    prepareForSegue("backToFirstSegue", sender: self)


}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "backToFirstSegue"{
        let vc = (segue.destinationViewController as! FirstImageVC)
        vc.Delegate = self //Include this line
        vc.chosenGhostPhoto = bgGhostImage?.image
    }
}

EDIT 1: Here's what I did to get the unwind to work, though it's not carrying back the image selected from the collection view, which needs to be called chosenGhostPhoto.

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {


    performSegueWithIdentifier("unwindToFirstSegue", sender: self)


}

Upvotes: 2

Views: 2133

Answers (2)

Abhinav
Abhinav

Reputation: 38162

This is how you can do this:

Step 1: Add a delegate variable on your second view controller. Please name it delegate and not Delegate - owing to naming convention.

Step 2: Define a protocol and add functions that you want your second view controller delegate to perform. Say func selectedImage(image : UIImage)

Step 3: While pushing second view from first view controller, set your first view controller as delegate of second view controller. Something like this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "secondSegue"{
        let vc = (segue.destinationViewController as! SecondImageVC)
        vc.delegate = self
    }
}

Step 4: In your second view controller once image is selected call delegate of second view controller. Below function needs to be triggered on tap on the image on second view controller.

fun imageSelected {
   self.delegate.selectedImage(bgGhostImage?.image)
}

Step 5: Implement selectedImage in your first view controller and use the passed image.

Upvotes: 3

Santosh Gurram
Santosh Gurram

Reputation: 1017

An unwind segue (sometimes called exit segue) can be used to navigate back through push, modal or popover segues (as if you popped the navigation item from the navigation bar, closed the popover or dismissed the modally presented view controller). On top of that you can actually unwind through not only one but a series of push/modal/popover segues, e.g. "go back" multiple steps in your navigation hierarchy with a single unwind action.

When you perform an unwind segue, you need to specify an action, which is an action method of the view controller you want to unwind.

For your reference this is quit similar question and might help in understanding unwind segue

Upvotes: 0

Related Questions