0

I'm doing a call to an API in my AppDelegate.m didFinishLauncingWithOptions method. The JSON retrieved will be translated into an NSArray of objects. I'd like to set a property of my first view controller to that array so that view controller can use the latitude and longitude properties of those objects to map out locations.

Something like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // array retrieved - in actual application, this is an API call and translation
    Obj *object1 = [[Obj alloc] init];
    Obj *object2 = [[Obj alloc] init];
    Obj *object3 = [[Obj alloc] init];

    NSArray *arrayOfObjectsToMap = [NSArray arrayWithObjects:object1, object2, object3, nil];

    // pass object array along to first view controller
    firstController.objectList = arrayOfObjectsToMap;

    return YES;
}

I'm having trouble figuring out how to set properties on the first controller, which was created in Storyboard. The self.window.rootViewController of the AppDelegate is of type UIViewController and my initial controller is of type MapViewController with an NSArray property akin to the objectListproperty in the example above.

H K
  • 1,215
  • 2
  • 16
  • 29

3 Answers3

0

You can use the following method to pass the data to the view controller using the segue.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"showDetailSegue"]){
        ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
        controller.isSomethingEnabled = YES;
    }
}

Please follow the below post for more details. Passing Data between View Controllers

Community
  • 1
  • 1
fishfries
  • 9
  • 3
  • I know that's how you pass data between controllers using segues. I believe this is a different situation. This is the AppDelegate class passing data to the very first controller - no segue between the AppDelegate and the root view controller exists as far as I understand. – H K Oct 15 '14 at 22:23
  • @fishfries you should improve your answer as it doesn't address the OP issue. – carlodurso Oct 26 '14 at 03:18
0

I've decided to address the issue by saving the objects created to Core Data (alternatively, I could've used a Singleton or some other storage) and creating my initial view controller to read objects from Core Data.

H K
  • 1,215
  • 2
  • 16
  • 29
0

Make sure that you import your view controller's header file:

#import "MapViewController.h"

Then, because you know that your initial view controller is of type MapViewController, just add a cast to the expression:

((MapViewController *)firstController).objectList = arrayOfObjectsToMap;

To make it more secure you can check if the view controller is really a MapViewController

if ([firstController isKindOfClass:[MapViewController class]]) {
    ((MapViewController *)firstController).objectList = arrayOfObjectsToMap;
}

Update

Another option would be a cast right where you assign your firstController variable:

MapViewController *firstController = (MapViewController *)self.window.rootViewController;
marcusficner
  • 915
  • 8
  • 13