arrays - Resetting saved map pins, iOS Swift -


i working on small map application, far have code drop map pins , save them remain once app reopened, class main view controller:

import uikit import mapkit import corelocation  class viewcontroller: uiviewcontroller, mkmapviewdelegate, cllocationmanagerdelegate, uisearchbardelegate, uipopoverpresentationcontrollerdelegate {      var location: cllocation!     let locationmanager = cllocationmanager()      @iboutlet weak var placesmap: mkmapview!     @iboutlet weak var addbutton: uibarbuttonitem!     @iboutlet weak var morestuff: uibutton!      // popover button action     @ibaction func morestuff(sender: anyobject) {         self.performseguewithidentifier("showmorestuff", sender:self)         morestuff.adjustsimagewhenhighlighted = false     }      @ibaction func addbutton(sender: anyobject) {         let annotation = mkpointannotation()         annotation.coordinate = cllocationcoordinate2d(latitude: self.placesmap.userlocation.coordinate.latitude, longitude: self.placesmap.userlocation.coordinate.longitude)         self.placesmap.addannotation(annotation)         self.locationmanager.startupdatinglocation()     }      // location function     func locationmanager(manager: cllocationmanager, didupdatelocations locations: [cllocation]) {         let location = locations.last         let center = cllocationcoordinate2d(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)         let region = mkcoordinateregion(center: center, span: mkcoordinatespan(latitudedelta: 0.004, longitudedelta: 0.004))         self.placesmap?.setregion(region, animated: true)         self.locationmanager.stopupdatinglocation()         let locationdictionary:[string:double] = ["latitude":center.latitude,"longitude":center.longitude]         var locationarray = [[string:double]]()         if nsuserdefaults.standarduserdefaults().objectforkey("locationarray") != nil {              locationarray = nsuserdefaults.standarduserdefaults().objectforkey("locationarray") as! [[string:double]]     }         locationarray.append(locationdictionary)         nsuserdefaults.standarduserdefaults().setobject(locationarray, forkey: "locationarray")         nsuserdefaults.standarduserdefaults().synchronize()     }      override func viewdidload() {         super.viewdidload()         self.locationmanager.delegate = self         self.locationmanager.desiredaccuracy = kcllocationaccuracybest         self.locationmanager.requestwheninuseauthorization()         self.locationmanager.startupdatinglocation()         self.placesmap?.showsuserlocation = true         if nsuserdefaults.standarduserdefaults().objectforkey("locationarray") != nil {             dictionary in nsuserdefaults.standarduserdefaults().objectforkey("locationarray") as! [[string:double]]{                 let center = cllocationcoordinate2d(latitude: dictionary["latitude"]!, longitude: dictionary["longitude"]!)                 let annotation = mkpointannotation()                 annotation.coordinate = center                 self.placesmap?.addannotation(annotation)             }         }     } 

i want add button allows pins (memories) reset @ once. button located on new scene , class, called "popoveroptions". have following code class should this, not seem functioning no pins disappear map once pressed user!

@iboutlet weak var resetmemories: uibutton!  @ibaction func resetmemories(sender: anyobject) {     func removestoredlocations(){         nsuserdefaults.standarduserdefaults().removeobjectforkey("locationarray")         nsuserdefaults.standarduserdefaults().synchronize()     } } 

any idea why pins aren't being removed? have ensured class linked correctly buttons outlet / action.

you clear out user defaults key, don't change map view on view controller shows pins. need call removeannotations remove annotations map.

you add viewwillappear method detects annotations have been deleted , remove them. this:

func viewwillappear(_ animated: bool) {   if nsuserdefaults.standarduserdefaults().objectforkey("locationarray") == nil    {     if let annotations = self.placesmap.annotations       self.placesmap?.removeannotations(annotations)   } } 

the code above written remove annotations map if entire annotations dictionary deleted userdefaults. way avoid reloading annotations every time view controller regains focus.

btw, said new scene called "popoveroptions." if scene presented in popover above might not work. (i don't think viewwillappear gets called on view controller when popover dismissed, don't remember sure.)


Comments