0

enter image description here enter image description here

I want to make an Flashcards App and the behaviour is that on the CoursesVC, the user can add courses and click on them. Then he gets the list with flashcards. There he can add more flashcards. The storage is managed by CoreData. When its clicked on the cell, I pass the data to the flashcards list with prepareForSegue. To add the flashcard, I had the same idea in mind, but it was not possible because the variable from the second view controller wasn't initialised, when prepareForSegue was created. Question: How can I pass a NSManagedObject from the first ViewController to the third ViewController in an appropriate way? (ugly way would be to let the view render before creating prepareForSegue)

The difference to questions like "how to pass data between ViewControllers" is that I have three ViewControllers. It won't work with using prepareForSegue at the first and at the second view controller, because when the prepareForSegue is created, the variable in the second VC is not defined yet, because the view is not initialised yet! Keep in mind that the segue from the second to the third view controller is "Present Modally" as "Page Sheet"!

Lucas
  • 104
  • 1
  • 12
  • 1
    Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Ratul Sharker Nov 15 '18 at 07:24
  • 1
    @lucas Use NotificationCenter to passing data.. – Harshad Patel Nov 15 '18 at 08:07
  • @RatulSharker I don't want to pass between two VC but rather three ones. The problem is that the view with the var is not initialised yet when prepareForSegue of the second VC is created... – Lucas Nov 16 '18 at 17:21
  • @HarshadPatel but its a one-to-one-to-one relationship and not one-to-many relationship – Lucas Nov 16 '18 at 17:23

1 Answers1

0

This is the solution basically: Swift : prepareForSegue with navigation controller

The problem was that the third view controller is embedded as a navigation controller. That is the reason why the prepareForSegue is different.

Solution is to use following prepareForSegue in the second VC:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let navigationVC = segue.destination as? UINavigationController, let myViewController = navigationVC.topViewController as? DViewController {
        myViewController.currentCourse = self.currentCourse
    }
}
Lucas
  • 104
  • 1
  • 12