1

as written in the subject i can't find a way to pass a CLLocation data from the initial view controller to the last view controller.

My app has a 4 view controller (A-B-C-D). In the initial view controller (A) I get the position that is stored in CLLocation *currentLocation. The D view controller must return a list based on the location stored in the view controller A currentLocation.

How do I get it?

I tried with the NotificationCenter but it doesn't work. Can someone help me?

MasKar
  • 21
  • 2

3 Answers3

2

I quickly think of 3 solutions:

  • Store the value into a Singleton ( not necessarily the AppDelegate )
  • Store the value into the User defaults (NSUserDefaults)
  • Pass the value through all view controllers when you instantiate them (with a @property declaration)
  • If your controllers are linked to the same navigation controller then you could fetch the rootViewController which could be your "A" controller.

As codebad said, we would need more details to help you in a better way :)

Kevin Delord
  • 2,498
  • 24
  • 22
  • mmm i see there are a lot of solution! anyway Controllers are linked in a navigation controller. So which is the right mode to pass this kind of data? – MasKar Jul 18 '14 at 17:40
  • Well it depends if you want to save it for ulterior use or not if so using the NSUserDefaults seems a good idea. Otherwise I suggest to create a Singleton class which just contain this data. It makes the code more clear and separate your view controller methods from your data/models/etc ones. http://stackoverflow.com/questions/5381085/how-to-create-singleton-class-in-objective-c – Kevin Delord Jul 21 '14 at 09:55
1

One option is to store the CLLocation value in AppDelegate at the first ViewController, then read from AppDelegate in the last ViewController.

You can code this like this:

AppDelegate.h

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


 @property CLLocation *gMyLocation;

ViewControllerA.m

 #import "AppDelegate.h"
 ///
 UIAppDelegate.gMyLocation = //calculated location value

ViewControllerD.m

 #import "AppDelegate.h"
 ///
 NSLog(@"%@", UIAppDelegate.gMyLocation)

Hope it helps.. more information on your application would be needed for more advanced implementation, because using AppDelegates as singleton could be bad design habit.

nzs
  • 3,252
  • 17
  • 22
0

Take a look at this singleton LocationController.

https://github.com/hackerinheels/locationcontroller

With this, you can get the location anywhere with just one call.

CLLocation *currentLocation = [[LocationController sharedLocationInterface] getCurrentLocation];

Make sure to initialize location controller in the appdelegate by calling

[LocationController sharedLocationInterface] 
hackerinheels
  • 1,141
  • 1
  • 6
  • 14