I'm a beginner in swift and I'm making a converter app in IOS and I need a conversion historic, so everytime I do one convertion I want to pass the result to another viewcontroller. I tried with files but no success, then I tried with Segue and Prepareforsegue but the problem is that I just want to store the data I don't want to show the viewcontroller. Anyone could help me? Thanks in advance
1 Answers
When you use storyboards then hold a reference to the called view controller within prepareForSegue.
When you don't use storyboards then you create the called view controller yourself and there you can pass any data to the called view controller by calling its public setters. (Create them if needed)
Handing data back to the calling view controller is a bit more tricky when you want to avoid that their classes are strongly coupled. And it is a good OOP pattern to avoid that.
For that declare a protocol that the calling VC has to implement (conform to). And implement the protocol. In the called VC declare a reference to an object of that very protocol. (not a reference to the calling VC's class but to the protocol). You may want to call the reference delegate or xyDelegate when xy is a meaningful name for the purpose of the protocol. This is because this pattern is called the delegate pattern.
The delegate pattern is very common in the cocoa and cocoa touch frameworks. Therefore It is worth implementing this the proper way, avoiding strong coupling of classes, to get used to it.
And btw, when you try the search on SO then you will find 1000ds of answers to this question.
As you don't want to show the view controller directly then you may want to familirarize yourself with instantiateViewControllerWithIdentifier
and storyboardWithName
to get a reference to the storyboard.

- 14,039
- 5
- 48
- 71