Why does [[UIApplication sharedApplication] delegate]; return a nil object

Go To StackoverFlow.com


I have an iphone application with multiple views and related controllers and xib files. In the controller for the first view that is loaded I am trying to access the delegate for the application but the object that is returned is nil

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

Any ideas why it would be nil?

2009-06-16 10:01
by Liam


Because it is not (yet) set. The UIApplication delegate is usually instantiated and set from the MainWindow nib file. If you access it before the nib is loaded completely (e.g. an init method in a controller in the same nib), it is nil.

2009-06-16 10:41
by Nikolai Ruhe
Thanks Nikolai, I moved the accessing of the delegate to a point where the application was fully loaded and it was no longer nil - Liam 2009-06-16 10:59