1

I've been at this problem for a few days can't crack it. Here is an overview for context.

User starts at HomeViewController which is a tabbed view. Tap button activates segue to SketchViewController which is not tabbed. User sketches an image. User taps Done button to indicate sketch is finished. Image is saved to small, hidden, UIImageView on SketchViewController. I want to pass this image to HomeViewController, where it will be used.

Here is the SketchViewController where the image starts:

class SketchViewController: UIViewController {

@IBOutlet weak var SketchHiddenImage: UIImageView!

@IBAction func DoneButtonTapped(sender: AnyObject) {
//screenshot taken which results in the following
SketchHiddenImage.image = image
}

// Pass image to HomeViewController

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "toTabController" {
    let tabbar: UITabBarController = segue.destinationViewController as! UITabBarController
        let DestViewController: HomeViewController = tabbar.viewControllers?.first as! HomeViewController

 DestViewController.sketchImage = SketchHiddenImage.image
}
}

Here is the HomeViewController where I want the image to end up:

class AskViewController: UIViewController {

@IBOutlet weak var HiddenTransferImage: UIImageView!

var sketchImage = UIImage!()
}

I have a storyboard "show" segue from SketchViewController to TabViewController with identity "toTabController".

I have seen all of the other similar posts, but this situation has a few complications for example the TabBarController and no amount of amalgamation and manipulation of those other posts has helped me fix this so far. Anyways, thank you for the guidance.

AVI
  • 5,516
  • 5
  • 29
  • 38
  • A note for future people referencing this, AskViewController = HomeViewController, sorry about that –  Nov 30 '15 at 03:15

1 Answers1

0

You don't want to use a normal segue to return to your HomeViewController because it creates a new view controller. Instead, you want to use an unwind segue.

Look at this question for how to set up an unwind segue:

How to set up unwind segue

You can use prepareForSegue with an unwind segue. The destination view controller will be the view controller that you are returning to (that is, the one that called SketchViewController in the first place). Once you have the calling view controller, save your image to it:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "returnToHomeViewController" {
        let homeVC = segue.destinationViewController as! AskViewController
        homeVC.sketchImage = SketchHiddenImage.image
    }
}
Community
  • 1
  • 1
vacawama
  • 150,663
  • 30
  • 266
  • 294