tags:

views:

1581

answers:

1

Hello,

I am experiencing a strange behaviour. In a view controller I would like to keep a reference on my app delegate ([[UIApplication sharedApplication] delegate])

So I have a property I set when my controller is instantiated. All is working on the simulator by on the device my property is always 0x0 in debugger.

I wrote this code to test :


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {  
     AppDelegate *localVar = [[UIApplication sharedApplication] delegate]; // OK
     self.appDelegate = [[UIApplication sharedApplication] delegate]; // 0x0
    }
    return self;
}

I am sure that the appDelegate is set, I also write a setter to verify that it is called


- (void)setAppDelegate:(AppDelegate *)delegate
{
    appDelegate =  delegate; // delegate is a valid address but appDelegate is still showing 0x0
}

I am wondering if my AppDelegate class is well written.

Do you have an idea ?

I am lost...

Thanks

A: 

At the time your view controller is loaded, the delegate is probably not yet instantiated or set in the UIApplication object. All of this is done in nib loading, but the order is arbitrary.

Nikolai Ruhe