重置保存的地图引脚,iOS Swift



我正在开发一个小型地图应用程序,到目前为止,我已经有了删除地图引脚并保存它们的代码,以便在应用程序重新打开后它们仍然存在,这个类用于主视图控制器:

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

我想添加一个按钮,允许所有引脚(内存)一次重置。此按钮位于一个名为"PopoverOptions"的新场景和类上。我从这个类中得到了以下代码,应该可以做到这一点,但它似乎不起作用,因为一旦用户按下它,就不会有引脚从地图上消失!

@IBOutlet weak var resetMemories: UIButton!
@IBAction func resetMemories(sender: AnyObject) {
    func removeStoredLocations(){
        NSUserDefaults.standardUserDefaults().removeObjectForKey("locationArray")
        NSUserDefaults.standardUserDefaults().synchronize()
    }
}

你知道为什么没有把别针取下来吗?我已经确保类以及按钮出口/动作都被正确链接。

清除用户默认键,但不要更改显示这些引脚的视图控制器上的映射视图。您需要调用removeAnnotations来从地图中删除注释。

您可以添加一个viewWillAppear方法来检测您的批注是否已被删除并将其删除。类似这样的东西:

func viewWillAppear(_ animated: Bool)
{
  if NSUserDefaults.standardUserDefaults().objectForKey("locationArray") == nil 
  {
    if let annotations = self.placesMap.annotations
      self.placesMap?.removeAnnotations(annotations)
  }
}

上面的代码只用于在从userDefaults中删除了整个注释字典的情况下从映射中删除注释。这样可以避免每次视图控制器重新获得焦点时重新加载注释。

顺便说一句,你说你的新场景名为"弹出选项"。如果场景是以弹出方式呈现的,那么以上可能不起作用。(我不认为当popover被解除时,viewWillAppear会在视图控制器上被调用,但我不太记得了。)

最新更新