84

I have a project that is based on the Navigation Based Application template. In the AppDelegate are the methods -applicationDidFinishLoading: and -applicationWillTerminate:. In those methods, I am loading and saving the application data, and storing it in an instance variable (it is actually an object-graph).

When the application loads, it loads MainWindow.xib, which has a NavigationConroller, which in turn has a RootViewController. The RootViewController nibName property points to RootView (my actual controller class).

In my class, I wish to refer to the object that I created in the -applicationDidFinishLoading: method, so that I can get a reference to it.

Can anyone tell me how to do that? I know how to reference between objects that I have created programmatically, but I can't seem to figure out to thread my way back, given that the middle step was done from within the NIB file.

Chris Hanson
  • 54,380
  • 8
  • 73
  • 102

4 Answers4

204

For variables (usually the model data structure) which I need to access it anywhere in the app, declare them in your AppDelegate class. When you need to reference it:

YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
//and then access the variable by appDelegate.variable 
leonho
  • 3,563
  • 2
  • 21
  • 16
  • 7
    I voted this one up because you added the cast, you need to cast AppDelegate to your own type so you can see your own properties you set. You could edit to make this more clear by not casting to "AppDelegate" though... – Kendall Helmstetter Gelner Oct 24 '08 at 03:32
  • Is there a way to access the root view controller without making reference to the app delegate name or any of its members? (I'd like to write some portable code that needs the root view controller.) – Ken Feb 27 '11 at 11:00
  • SK9, every iOS app has to have an appDelegate, so the code will be portable! – Daniel Sep 13 '13 at 00:40
16

If I understand your question, you want to reference member variables/properties in your AppDelegate object? The simplest way is to use [[UIApplication sharedApplication] delegate] to return a reference to your object.

If you've got a property called window, you could do this:

UIWindow   *mainWindow = [[[UIApplication sharedApplication] delegate] window];
//do something with mainWindow
Anish Gupta
  • 2,218
  • 2
  • 23
  • 37
Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
11

Here's a well defined portable alternative for iOS4.0 and up:

UIApplication *myApplication = [UIApplication sharedApplication];
UIWindow *mainWindow = [myApplication keyWindow];
UIViewController *rootViewController = [mainWindow rootViewController];

or, in one line,

UIViewController *rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];

Don't forget to set the window's rootViewController property (say in IB) or this will do jack.

Ken
  • 30,811
  • 34
  • 116
  • 155
2

Define a macro and use it anywhere!

#define appDelegateShared ((AppDelegate *)[UIApplication sharedApplication].delegate)

In My Code:-

UIViewController *rootViewController = appDelegateShared.window.rootViewController;
Community
  • 1
  • 1
tryKuldeepTanwar
  • 3,490
  • 2
  • 19
  • 49