Referencing an iPhone App’s UIApplicationDelegate from a nested view

It is useful to know that you can reference the top level of your application from anywhere since it is a Singleton.  This enables access to the application delegate as follows:

[[UIApplication sharedApplication] delegate];

This can be useful if you want declare your model at the top level of your app and need to reference it from other places.  In this case you would need to cast the delegate reference:

StemStopwatchAppDelegate* ref =
(StemStopwatchAppDelegate*) [[UIApplication sharedApplication] delegate];

I have also found it necessary to reference UIApplicationDelegate in a view with a UITabBarController when I wanted to use a UIActionSheet (a bit like a popup dialog).  This ensured that the actionsheet had focus and not the tabbar underneath when it was invoked:

[actionSheet showInView:ref.window];

Tags: , , , ,

This entry was posted on Friday, January 29th, 2010 at 6:09 pm and is filed under iPhone, objective-c. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

 

Leave a Reply

You must be logged in to post a comment.