0

I have initialized an delegate object in ViewDidLoad of my ViewController, but when I am again loading it, it is initializing the value again.

I am saving some some sort of array in that delegate object which I want to access using getObject and setObject. What should I do so that the delegate object doesn't get re-initialized every time ViewDidLoad is called?

Kheldar
  • 5,361
  • 3
  • 34
  • 63
Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154

4 Answers4

1

Have you considered this strategy:

  • After your app is launched, before that specific object is initialized and used, set it to nil.
  • For the first time your app is trying to use it, check if it's still nil (it should since it's the first time), then initialize it and use it
  • For the rest of your app's life cycle, whenever your app runs into the viewDidLoad method again, always check whether that object is nil or not (it should not be nil at this point). This would save your app the time and efforts trying to initialize an object which was already initialized.

However, when you use this strategy, you should be aware that that specific object's value should stay the same throughout your app's life cycle. Otherwise it won't work.

Di Wu
  • 6,436
  • 3
  • 35
  • 51
  • I did like - (void)viewDidLoad { [super viewDidLoad]; if (myRunningObject == nil) { NSLog(@"If executed"); myRunningObject = [[ResAppDelegate alloc] init]; }, but yet is it not working, one more thing I would like to mention that I am doing something like -(void) setObjectforNew: (ResAppDelegate *)myRunningObject { NSLog(@"setObjectForNow"); self.myRunningObjectNew = myRunningObject; } then sending array usring new object, which is in another class, am I doing it right? – Chatar Veer Suthar Feb 07 '11 at 09:02
0

You should initialize only UI elements in viewDidLoad. Everything else should be inited in constructor (initWith...)

Max
  • 16,679
  • 4
  • 44
  • 57
  • but what should I do when I want to use one object that will save my data and I will just pass to other classes and use its data there?? – Chatar Veer Suthar Feb 07 '11 at 08:48
  • If you want some array/data/object etc to be accessible from any place in your application, then the best way to do this would be creating a singleton, that would store all the necessary data. – Max Feb 07 '11 at 08:56
  • what is singleton Sir, I haven't heard this term before, just give me hint – Chatar Veer Suthar Feb 07 '11 at 08:58
  • http://en.wikipedia.org/wiki/Singleton_pattern for example, when you are calling [UIApplication sharedApplication], [UIDevice currentDevice], [UIScreen mainScreen] you are calling singleton. – Max Feb 07 '11 at 09:00
0

If you want someplace that things are only done once, that's generally in a Singleton somewhere - an object that is made once, and referenced from all over.

The AppDelegate is a default singleton you get for free. But if you decide after a while too much is going in the AppDelegate, it's a good idea to make different Singleton objects into which you put custom data.

There are many examples all over showing how to make a Singleton, now that you know the term you are looking for.

Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
0

As everybody else is saying, you probably need a singleton object. The easiest way to do this is like so:

// interface

@interface MyViewController
{ ... }

+(DelegateType*) theDelegate;

...

@end

// implementation

@implementation MyViewController

+(DelegateType*) theDelegate
{
    static DelegateType* theDelegate = nil;

    if (theDelegate == nil)
    {
        theDelegate = [[DelegateType alloc] init];
    }
    return theDelegate;
}

@end

// To use it 

    [foo setDelegate: [MyViewController theDelegate]];
JeremyP
  • 84,577
  • 15
  • 123
  • 161