0

I have the following setup in my storyboard in a Swift project:

enter image description here

I have an array containing many UIColor:

let palette = [UIColor.greenColor(), UIColor.redColor(), ...]

The user can click on the "Button" Tab bar button, and the second VC will be presented modally (vertical cover). From there he can choose a color from the collection view. The first VC is a UIViewController, the second is a UICollectionViewController. In the second VC I have the following code to handle color selection:

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    println("select color with index \(indexPath.row)")
    //user selected color, dismiss modal view controller
    self.dismissViewControllerAnimated(true, completion: nil)
}

How can I pass back the selected color to my first view controller? I tried:

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    let vc = self.storyboard?.instantiateViewControllerWithIdentifier("FirstViewController") as FirstViewController
    // now, in FirstViewController, set backgroundColor of backgroundView with user selected value
    vc.backgroundView.backgroundColor = palette[indexPath.row]
    //user selected color, dismiss modal view controller
    self.dismissViewControllerAnimated(true, completion: nil)
}

Above code gives me Unexpectedly found nil while unwrapping Optional value

It seems even when instantiating, backgroundView.backgroundColor is not available.

I also tried to execute the above code in the completion block of dismissViewController, with the same error:

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    //user selected color, dismiss modal view controller
    self.dismissViewControllerAnimated(true, completion: {
    let vc = self.storyboard?.instantiateViewControllerWithIdentifier("FirstViewController") as FirstViewController
    // now, in FirstViewController, set backgroundColor of backgroundView with user selected value
    vc.backgroundView.backgroundColor = palette[indexPath.row]
    })
}

Thanks a lot for any suggestions, I am really loosing my head over this. Please let me know if anything is unclear, I will be happy to provide more information.

Camillo
  • 544
  • 1
  • 6
  • 24
  • `instantiateViewControllerWithIdentifier` will make a *new* `FirstViewController`. You need a reference to the one you already have. – Aaron Brager Feb 09 '15 at 09:37
  • Hi Aaron and thanks for your comment. Do I have to implement delegate to reference the FirstViewController? I found this answer to another question: http://stackoverflow.com/a/25204814/3778854 - See the second point. Would this be a valid way to solve my problem? If not, can you tell me about more on how to achieve this in my situation? Anyway, I'll try this tonight - thanks again! – Camillo Feb 09 '15 at 11:11

1 Answers1

1

This blog post including full source code in Objective-C helped me to solve my issue. Relevant code snippets translated to Swift:

@IBAction func unwindToSegue (segue : UIStoryboardSegue) {

    if segue.sourceViewController.isKindOfClass(BackgroundColorCollectionViewController) {
        let vc = segue.sourceViewController as BackgroundColorCollectionViewController
        if (vc.selectedColor != nil) {
            self.view.backgroundColor = vc.selectedColor
        }
    }

}

-

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    println("select color with index \(indexPath.row)")
    self.selectedColor = palette[indexPath.row]
    self.performSegueWithIdentifier("colorSelected", sender: self)
}

If you are confused, be sure to watch the videos for a helpful explanation on how to connect the unwind segue.

Camillo
  • 544
  • 1
  • 6
  • 24