Search code examples
swiftxcode6userlocation

update location from another view controller


I want to continue to update userLocation every 0.5s from viewController to anotherViewController!! I've tried using a segue but that only updates one, anyone know another method or how?

Thanks!


Solution

  • Sure.

    Create a protocol. Let's call it MyLocationDelegateProtocol.

    Give it a method to get location updates.

    Have your anotherViewController implement the method, and add the MyLocationDelegateProtocol to the class definition. (These steps can be referred to as "making the class conform to the MyLocationDelegateProtocol".)

    Give the view controller that will be sending out location updates a locationDelegate property. When you create the view controller, set it's locationDelegate property to your anotherViewController.

    Write the location methods so that they check to see if the locationDelegate property is nil. If it's NOT nil, send location messages to the locationDelegate using the message(s) defined in the MyLocationDelegateProtocol.