Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Pierre Smith
11,842 PointsSwift. I'm trying to get my locationManager to update users location work in applicationDidBecomeActive.
So i'm trying to get my users location every time the application becomes active. I initiated my location manager in the mainViewController.swift and have all the functionality in the locationManager: didUpdateLocation method so I want to access this location manager in my appDelegate.swift file but I can't seem to figure out how to get this done.
My first attempt was using inheritance like this: appDelegate: mainViewController, UIApplicationDelegate { }
which allowed me to use the locationManager I initiated but it never actually seemed to call the locationManager:didUpdateLocation method because I see no changes made to my view nor in debug
My second attempt was creating a instance of the class "mainViewController" and it just allows for the methods to be called and on top of that it wants it to be called in a manner that is beyond me.
example:
var viewController = mainViewController()
mainViewController.locationManager(self)
why can't I use the locationManager I initiated in my main view controller like this
mainViewController.locationManager.startUpdatingLocation()
and how do I achieve this in the applicationDidBecomeActive method.
1 Answer

Stone Preston
42,016 PointsYou are currently creating a new MainViewController instance. What you want to do is retrieve the current instance of MainViewController the app is using, not create a totally new one
is your main view controller the root view controller of your app? if so you can access it from within the app delegate and cast it to your MainViewController like so:
var rootViewController = self.window!.rootViewController as MainViewController
that will retrieve the instance of the main view controller you need, instead of creating a totally new one like you currently are trying to do.
then you should be able to access the locationManager:
var rootViewController = self.window!.rootViewController as MainViewController
rootViewController.locationManager.startUpdatingLocation
Pierre Smith
11,842 PointsPierre Smith
11,842 Pointsok thank you. So I tried putting that and I'm given a AppDelegate -> 0 ->AppDelegate does not have member named 'window'.
And how to I check which view controller is my root view controller
Stone Preston
42,016 PointsStone Preston
42,016 Pointsin your storyboard your root view controller is the one with the arrow next to it. its the first view controller in the app.
hmm im not sure why you are getting that error, the app delegate should have an optional window property. you are using storyboards correct?
Pierre Smith
11,842 PointsPierre Smith
11,842 Pointsyes I am. then yes it is my root controller
Pierre Smith
11,842 PointsPierre Smith
11,842 PointsSorry I decided to try it again and it worked. Turns out It has to be created in one of the methods and not made globally for some reason.