1

I'm new to iPhone Development so bear with me. I'm trying to access an array stored in the AppDelegate from another view controller but when I do it tells me the array is empty.

I created an NSMutableAarray in the AppDelegate called containerTypeArray. I build the array with objects in the AppDelegate method called:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

After I initialize and init with objects the size of my array is 92 confirmed by the following NSLog message:

NSLog(@"container type size %i", [containerTypeArray count]);

I try to reference the array variable in a different view controller. In this view controller I import the appdelegate.h file and write the following in the view did load method:

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
     NSLog(@"count at load %i", [appDelegate.containerTypeArray count]);

This time the NSLog count is 0.

Can some one tell me what I'm doing wrong? Thanks!

Amar
  • 13,202
  • 7
  • 53
  • 71
tanya
  • 117
  • 7

1 Answers1

2

This is because application:didFinishLaunchingWithOptions is called after viewDidLoad.

The documentation for application:didFinishLaunchingWithOptions:

You should use this method (and the corresponding application:didFinishLaunchingWithOptions: method) to initialize your application and prepare it to run. This method is called after your application has been launched and its main storyboard or nib file has been loaded, but before your app’s state has been restored. At the time this method is called, your application is in the inactive state.

So the view is loaded before the application:didFinishLaunchingWithOptions method is called.

There's a related SO question on the timing of these methods.

Community
  • 1
  • 1
Cody A. Ray
  • 5,869
  • 1
  • 37
  • 31