-5

I have several view controllers, which needs to use some common variables, It seems that I have several ways to do that

1) Global variable defined in AppDelegate

2) Use Segue to pass objects between

3) Use delegate pattern?

Could you let me know which way is better?

Adam Lee
  • 24,710
  • 51
  • 156
  • 236

3 Answers3

1

You could use the AppDelegate, but if you need to do some operations as well, you should use singletons. You can read about them here.

Levi
  • 7,313
  • 2
  • 32
  • 44
1

All depends on your needs. If your variables are dynamic and you want to notify all classes using them you could use delegate pattern or a notifications passing your variables as objects. If you app requires some additional calculations or setters you might want to create a singleton class and then create delegate to pass some info when needed. Example would be using CLLocationManager to make some calculations and then only passing values required to other classes like distance traveled or setting a distance property of your class that can be accessed from other class members etc.

Pancho
  • 4,099
  • 1
  • 21
  • 32
0

If they are used by the more than one class it is better to use AppDelegate.

Manpreet Singh
  • 181
  • 1
  • 9