views:

191

answers:

1

Hello,

first of all, sorry if that question is dumb but I´m a total newbie in ObjC. In my iPhone Application, I have an CLLocationManager implementation in my one and only ViewController because I have only one View. No I want more Views in my Application and every View needs the Position Information from the CLLocationmanager.

So, I think I have to move the implementation from the ViewController into my AppController, because I don´t want to implement CLLocationManager in every ViewController, correct?

But now, I really don´t know how the ViewCotrollers will get the Position Information? I thought of implementing a Singleton Class where I store the information and every VieController can get the Information from there. But for that I have to implement a timer or something like that in the ViewControllers so that they will check every second if new Data is available. That sound not really good and crappy.

How can the ViewControllers automatically get the new Position information? Maybe I need something like an observer but mayby there is something ready in Cocoa/ObjC? I think I´m on the wrong direction with my application.

Thank you so much for any help

twickl

+2  A: 

Here's a suggestion off the top of my head that might be useful as a starting point. You are right in thinking that you don't want to repeat code to setup the connection to the CLLocationManager within every ViewController. However, I wouldn't move that code into your AppController (I'm assuming you mean AppDelegate).

I would create a generic ViewController subclass and call it something like LocationAwareViewController. LocationAwareViewController would be responsible for creating a connection to the CLLocationManager and "signing up" for updates.

I would setup the LocationAwareViewController as a delegate of the CLCLocationManager. The LocationAwareViewController would then implement some or all of the CLLocationManagerDelegate methods. Now this ViewController will receive updates whenever new location information is available. You shouldn't have to setup any polling timers.

Each of your specific ViewControllers would then be subclasses of LocationAwareViewController. They could also override the CLLocationManagerDelegate methods to perform specific actions for the View(s) they are responsible for.

One disclaimer: I haven't had a chance to use Core Location yet so I'm basing my expectations of how CLLocationManager works on the documentation alone. It looks pretty straightforward, but maybe someone else can offer more information based on their experiences.

Jonathan Arbogast