ios - Swift delegate protocol not updating with locationManager -


i have 2 views childview communicates parentview. using google maps sdk track users location if startupdatinglocation() using button, protocol not work if initiate startupdatinglocation() app loads works fine. below two controller files.

i trying display location in label not sure why protocol not working after button click.

child:

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()     // additional setup after loading view, typically 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 resources can 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     } } } 

parent:

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 resources can 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)     } } } 

you setting mapchildcontroller self before segue view:

override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {     if segue.identifier == constants.embedsegue {         let mapchildcontroller = segue.destinationviewcontroller as! mapchildcontroller         mapchildcontroller.locationdelegate = self 

after set delegate self viewcontroller went out of scope, delegate nil again , never called. need set delegate in viewdidload


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -