Search code examples
iosobjective-cuiviewcontrollersingletonuistoryboardsegue

Passing data between several view controllers


I've created 7 view controllers and I've got several textfields in each one. I need to collect data from all view controllers and make API request in the last one.

Whats the best pattern of passing this data?

I was thinking about passing one custom model object via prepareForSegue method but it will be too complex for maintenance.

Also I was considering singleton/NSUserDefaults but it's not perfect as well.


Solution

  • Singleton is NOT the perfect way, as it messes up your memory management easily. Same with NSUserDefaults (+ it only eats certain classes and persists data that probably shouldn't be persisted).

    Just pass the object from view controller to view controller.

    if you find 7 view controllers to difficult to maintain, probably your users will find 7 view controllers difficult to deal with too.

    You could also have a common BaseViewController that passes an object in prepareForSegue: if the destination view controntroller is a also of kind BaseViewController.