0

I need to send an object (data) from a view controller (embedded in a navigation controller) to a class (or struct) which should manage this data. How can I get a reference to this viewController from the class (which has no link of any kind (no segue), or better the struct) with the set of viewControllers in order to implement a delegation protocol?

I use swift, a storyboard, and I look for any pointer or documentation which could help me to understand how to address this problem. I'm a beginner and I am sorry if this question is far too trivial.

I just need a link to appropriate documentation, many thanks.

Shivam Tripathi
  • 1,405
  • 3
  • 19
  • 37
jfboisvieux
  • 157
  • 1
  • 5
  • How does “a class (or struct)” get created? What object is responsible for creating and storing it? – rob mayoff Mar 22 '18 at 16:37
  • The class or struct is created independently of the view controllers, it's a helper class or a model class. I build it to manage the data that comes from the viewControllers and later to coordinate the flow. – jfboisvieux Mar 22 '18 at 16:42
  • Share your code how you are displaying you `ViewController` and in which class you have the object of your `DataManager` – Syed Qamar Abbas Mar 22 '18 at 16:45
  • 1
    “The class or struct is created independently of the view controllers”. So what? There are three possibilities: you have a global variable (singleton) instance of the helper class, or you have code somewhere that creates the instance, or you create the instance in a storyboard or xib. You need to edit your question to explain how the instance is created. – rob mayoff Mar 22 '18 at 16:47
  • It's a rather large code that implements a 24-hour food recall, but the basic is quite straightforward: I have several view controllers which segues to obtain data from the user. When one validated data is obtained (in the last view controller) I repeat the operation on some other piece of data. I want to create an independent class (or classes) which gets the data and start appropriate actions to validate and provide recommendations to the user. – jfboisvieux Mar 22 '18 at 16:57
  • Would you suggest to use a singleton for the model class ? – jfboisvieux Mar 22 '18 at 17:03

1 Answers1

0

This answer might help you. You might wanna read up more on delegation pattern in iOS. https://stackoverflow.com/a/42977875/2396199

Alejandro Vargas
  • 1,327
  • 12
  • 26
  • would you recommand to make a singleton class for an helper class ? – jfboisvieux Mar 22 '18 at 17:19
  • Based on your comments, if the class only perform validations then I would create class methods and would call those methods using class name. eg : Utilities.swift has method performValidation(). Then I would call Utilities.performValidation() – Alejandro Vargas Mar 22 '18 at 18:31
  • But from the question it seems like you want to delegate from helper to viewcontroller too, so creating a singleton would suffice. – Alejandro Vargas Mar 22 '18 at 18:38