1

In the project i am currently working upon, there are lots and lots of UIViewController objects (of some UIViewController subclass) are created and used. And believe me it was creating lots of issues. And I am working upon it (kind of refactoring).

As I see, most of the (those)objects required initialization only once and used multiple times. As I will be working on this project from now (and also the project is of long duration), How can I be sure that each of the UIViewController Subclass object is only one alive at a time.

I doubt if I should make all the UIViewControllers Singleton. And if so, How should I implement that. Meaning; Should I initialize all the objects in applicationDidFinishedLaunching:WithOptions or where?

Another Question is: (As I think might not be true) Should all the UIViewController in project be singleton?

Popeye
  • 11,839
  • 9
  • 58
  • 91
viral
  • 4,168
  • 5
  • 43
  • 68
  • Just keep in mind that if you end up using a `singleton` pattern approach, that your view controllers will stay in memory for the life of the app, potentially causing memory problems if not managed correctly (depending on the memory footprint of your view controllers) – danielbeard Jun 21 '12 at 07:59
  • @danielbeard Yes, you mentioned a very important point. – san Jun 21 '12 at 08:08
  • Singleton would be fine approach if used properly....otherwise, you may end up using too much memory..... I have had experienced it... :) – Krishna Jul 19 '12 at 17:10

2 Answers2

1

If you want to ensure that all the UIViewController objects in the project are being created only once then only way is Singleton. And you need not to intialize them in applicationDidFinishLaunching. You can intialize them any where(usually where you need them).

Go to link for creating singleton properly: http://cocoasamurai.blogspot.in/2011/04/singletons-your-doing-them-wrong.html

Source : Make UIViewController a singleton?

Community
  • 1
  • 1
san
  • 3,350
  • 1
  • 28
  • 40
0

Well making them singleton if you want them to be only one object of each subclass is not a bad idea,

if you made them singletons, don't initialize them in applicationDidFinishedLaunching:WithOptions but initialize them as soon as you need them (read more about lazy initialization)

However i would suggest to make each webView a property of your Appdelegate, such that whenever you need them you will get them from the appDelegate

Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56