我正在开发一个小型地图应用程序,到目前为止,我有代码删除地图引脚并保存它们,以便在重新打开应用程序后它们保留,此类用于主视图控制器:
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()
}
}
知道为什么没有移除引脚?我已确保该类正确链接以及按钮outlet / action。
答案 0 :(得分:1)
清除用户默认密钥,但不要更改显示这些引脚的视图控制器上的地图视图。您需要调用removeAnnotations从地图中删除注释。
您可以添加一个viewWillAppear方法,检测您的注释是否已被删除并删除它们。像这样:
response.data
如果从userDefaults中删除了整个注释字典,则编写上述代码仅从地图中删除注释。这样,每次视图控制器重新获得焦点时,都可以避免重新加载注释。
顺便说一下,你说过你的新场景叫做#34; PopoverOptions。"如果场景以弹出框形式显示,则上述内容可能无效。 (我不认为当一个弹出窗口被解除时,viewWillAppear会被一个视图控制器调用,但我肯定不会记得。)