2016-03-28 16 views
1

Ich mache eine kleine Kartenanwendung. Ich möchte die Pins (Speicher) zurücksetzen können, der Benutzer ist gefallen. Ich möchte dies durch eine "Reset memories" Taste durchgeführt werden, über Popover angezeigt:Kartenpins mit Knopf zurücksetzen über Popover

enter image description here

Ich habe diesen Code für meine Haupt-View-Controller-Klasse, die die Karte usw. behandelt:

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 { 
      for 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) 
      } 
     } 
    } 

Hier ist der Ausgang und die Aktion für die Schaltfläche Speicher zurücksetzen, dieser Code wird in einer neuen Klasse für das Popover namens "PopoverOptions" gespeichert. Zur Zeit, wenn die Taste gedrückt wird, werden keine Pins von der Karte entfernt:

@IBOutlet weak var resetMemories: UIButton! 

@IBAction func resetMemories(sender: AnyObject) { 
    func removeStoredLocations(){ 
     NSUserDefaults.standardUserDefaults().removeObjectForKey("locationArray") 
     NSUserDefaults.standardUserDefaults().synchronize() 
    } 
} 

Jede Hilfe dieser Funktion erhalten geschätzt wird, ich bin neu in Swift

Antwort

0

Sie die Unterstützung Array aktualisieren, aber nicht die Karte sich selbst ansehen. Füge placesMap.removeAnnotations(placesMap.annotations) zu resetMemories() hinzu.


aktualisiert: Da die Aktion in einer anderen Datei ist, feuert eine Benachrichtigung von resetMemories():

NSNotificationCenter.defaultCenter().postNotificationName("memoriesUpdated", object: nil) 

Dann in Ihren View-Controller für die Benachrichtigung hören und aktualisieren die Stifte:

NSNotificationCenter.defaultCenter().addObserverForName("memoriesUpdated", object: nil, queue: nil) { [weak self] (n) -> Void in 
    // remove existing annotations 
    if let annotations = self?.placesMap.annotations { 
     self?.placesMap.removeAnnotations(annotations) 
    } 

    // add new annotations (if any) 
    if NSUserDefaults.standardUserDefaults().objectForKey("locationArray") != nil { 
     for 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) 
     } 
    } 
} 
+0

Danke für deine Antwort :) Ich habe deinen Vorschlag ausprobiert, aber da die resetMemories() Funktion in einer neuen Klasse ist, kann sie placesMap nicht sehen! Hast du noch einen Vorschlag? – Oscar

+0

Sie können eine Benachrichtigung von 'resetMemories()' auslösen und dann die Anmerkungen aktualisieren, wenn Sie diese Benachrichtigung erhalten. Ich werde meine Antwort bearbeiten. – jszumski

+0

Danke nochmal, ich werde das jetzt versuchen. Der View Controller Code sollte in die ViewDidLoad Funktion korrekt eingefügt werden? – Oscar