Swift委托协议不使用locationManager更新

时间:2015-06-01 05:37:59

标签: ios swift

我有2个childViewparentView进行通信的观看次数。我使用Google maps SDK来跟踪用户位置,但是如果我startUpdatingLocation()使用按钮,则协议不起作用,但是如果我在应用加载后立即启动startUpdatingLocation(),则工作正常。以下是我的Two controller文件。

我只是想在标签中显示位置但不确定按钮点击后协议无法正常工作的原因。

子:

import Foundation
import UIKit

protocol LocationUpdateDelegate{
func delegateUserLocation(mapChildController:MapChildController, userLocation: String)
}

class MapChildController: UIViewController, CLLocationManagerDelegate
{

@IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
var didFindMyLocation = false
var myLocations:[String] = []
var alreadyUpdatedLocation = false
var locationDelegate:LocationUpdateDelegate?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    var camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 15, bearing: 0, viewingAngle: 45)
    var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)

    mapView.myLocationEnabled = true
    self.view = mapView

    if NSProcessInfo().isOperatingSystemAtLeastVersion(NSOperatingSystemVersion(majorVersion: 8, minorVersion: 0, patchVersion: 0)) {
        println("iOS >= 8.0.0")
        locationManager.requestWhenInUseAuthorization()
    }

    mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
    if !didFindMyLocation {
        let myLocation: CLLocation = change[NSKeyValueChangeNewKey] as! CLLocation
        var mapView = self.view as! GMSMapView
        mapView.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 15.0)
        mapView.settings.myLocationButton = true
        didFindMyLocation = true
    }
}

func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {
    var locValue:CLLocationCoordinate2D = manager.location.coordinate

    myLocations.append("\(locValue.latitude)/\(locValue.longitude)")
    self.locationDelegate?.delegateUserLocation(self, userLocation: "From child")
    println("Child location: \(locValue.latitude)/\(locValue.longitude)")
}

func trackingToggle()
{
    if !alreadyUpdatedLocation
    {
        locationManager.delegate = self
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()

        println("Tracking started")
        alreadyUpdatedLocation = true
    }
    else
    {
        locationManager.stopUpdatingLocation()

        println("Tracking stopped")
        alreadyUpdatedLocation = false
    }
}
}

父:

import UIKit

struct Constants {
static let embedSegue = "embedSegue"
}

class MapMainController: UIViewController, LocationUpdateDelegate
{
let mapChild = MapChildController()
var alreadyUpdatedLocation = false;
@IBOutlet weak var startTrackingButton: UIButton!
@IBOutlet weak var MapStatsView: UIView!
@IBOutlet weak var txtUserLocation: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == Constants.embedSegue {
        let mapChildController = segue.destinationViewController as! MapChildController
        mapChildController.locationDelegate = self
    }
}

func delegateUserLocation(mapChildController: MapChildController, userLocation: String) {
    println("Parent location: \(userLocation)");
    txtUserLocation.text = userLocation
}

@IBAction func startTrackingTapped(sender: UIButton) {
    mapChild.trackingToggle();

    if !alreadyUpdatedLocation
    {
        alreadyUpdatedLocation = true
        startTrackingButton.setTitle("Stop Tracking", forState: UIControlState.Normal)
    }
    else
    {
        alreadyUpdatedLocation = false
        startTrackingButton.setTitle("Start Tracking", forState: UIControlState.Normal)
    }
}
}

1 个答案:

答案 0 :(得分:0)

您在转到另一个视图之前将mapChildController设置为自我:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == Constants.embedSegue {
        let mapChildController = segue.destinationViewController as! MapChildController
        mapChildController.locationDelegate = self

在您将delegate设置为自我后,viewController超出范围,现在您的delegate再次为零,永远不会被调用。 您需要在delegate

中设置viewDidLoad