-3

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.

tailec
  • 591
  • 4
  • 20
  • 2
    Why do you consider singleton as not perfect ? – KIDdAe Feb 16 '15 at 15:26
  • are the UIViewControllers displayed in a modal sequence? i.e. one after another? – Kex Feb 16 '15 at 15:30
  • Singleton seems to be a good solution and probably I will use it but I wonder if there is another nicer solution to this problem. @Kex they are pushed – tailec Feb 16 '15 at 15:31
  • 1
    I think I know what you mean, you are concerned that each time you push a new controller you have to access the object again and modify the correct field, so it doesn't seem very elegant. It's probably better though than passing an object through the controllers via segue 7 times. – Kex Feb 16 '15 at 15:36
  • Hard to ask a question in stackoverflow these days because you always get minuses without knowing why. – tailec Feb 17 '15 at 08:20

1 Answers1

1

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.

vikingosegundo
  • 51,126
  • 14
  • 131
  • 172