1

On my view controller on the left, ModuleDetailsVC, I have a class property called moduleId. My problem is that I want to pass this property to ChartVC, in order to generate the corresponding graphic. I have a container view because I need a different view controller for my chart view.

class ModuleDetailsVC: UIViewController {
  @IBOutlet weak var chartSubView: UIView!
  private var _module: Module?

  [...]
}

class ChartVC: UIViewController {
  var moduleId: Int!

  func setModuleId(moduleId: Int) {
    self.moduleId = moduleId
  }

  [...]
}

Where to call ChartVC.setModuleId()? If calling this is the right solution... (I already tried ViewDidLoad/Appear / SubViewLayout... etc

PS: Title may be totally wrong, if you have any idea for it, just edit.

enter image description here

  • This will help http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/ and http://stackoverflow.com/questions/17995799/pass-data-between-view-controller-segue and also http://stackoverflow.com/questions/20017026/passing-data-between-view-controllers-using-segue – Shoaib Aug 30 '15 at 10:18
  • Thanks, I will read this. By asking this I also wanted to confirm that my design was legit, that there wasn't a better option for doing this. Can you confirm me this, even if I know there are no 'perfect' solution –  Aug 30 '15 at 10:43
  • @Shoaib Ok I just the first link by now, and I already know this. I already use this kind of technique for my usual view to view data passing using segue. But for me the situation i described is not analog, because I am using a container view, and I already tried to use the segue for data passing without result. (I guess the segue isn't used as usual?) If you know how to use this segue enligthen me pls :) –  Aug 30 '15 at 10:49
  • Ok, finished reading the 2 other links, and they are about segues too, and it's stuff I already know. I just don't know how to apply it to this 'kind' of segue :/ –  Aug 30 '15 at 10:51
  • Can you share the code that displays `ChartVC` or explain how you wired it up in a storyboard? – hennes Aug 30 '15 at 10:54
  • There is nothing in code for ChartVC, as the picture show, there is a container, and I just put everything in the corresponding default view. I just set the 'custom class' of the container view to ChartVC. Did I answered your question? –  Aug 30 '15 at 10:57
  • @Mayerz sorry I didn't notice that container. I thought you were navigating the controller. Do you have instance of ChartVC in ModuleDetailsVC class? – Shoaib Aug 30 '15 at 11:03
  • Well no, I wish but it's instanciated by the storyboard. (But i have the container UIView) –  Aug 30 '15 at 11:09

1 Answers1

1

The view controller that will trigger the segue to show the container view is its parent view controller. In your case, that's the controller on the left - ModuleDetailsVC.

In order to pass in data to the container view's controller ChartVC, you just need to override prepareForSegue in the presenting controller (ModuleDetailsVC).

class ModuleDetailsVC: UIViewController {
    private var module: Module

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if let chartVC = segue.destinationViewController as? ChartVC {
            chartVC.setModuleId(module.id)
        }
    }
}

class ChartVC: UIViewController {
    var moduleId: Int!

    func setModuleId(moduleId: Int) {
        self.moduleId = moduleId
    }
}
hennes
  • 9,147
  • 4
  • 43
  • 63
  • Damn, I tried this at first, but I retrieved the segue with the segue identifier, and impossible to make it work. Is it because I couldn't do it with the segue identifier? Thanks a lot. –  Aug 30 '15 at 11:14
  • @Mayerz Using the segue identifier should actually work as well if you've set it up in the storyboard and didn't mistype it in your code. – hennes Aug 30 '15 at 11:18
  • Then my question is useless, I must have made a silly mistake because all this stuff is pretty trivial, was just my first time using ViewContainer, so I put the error on the count of it, and didn't triple check everything. Thanks –  Aug 30 '15 at 12:00