2016-03-26 8 views
0

私はこれまでのところ、私は地図上のアイコンをドロップすると、アプリがリニューアルオープンされた後、彼らは残っているので、それらを保存するためのコードを持っている、小さなマップアプリケーションに取り組んでいます、このクラスは、メインビューコントローラ用です。リセット保存された地図上のアイコン、iOSのスウィフト

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

なぜピンが削除されていないのでしょうか?クラスが正しく連結されていることと、ボタンのアウトレット/アクションが確実に確保されています。

答えて

1

あなたは、あなたのユーザデフォルトキーアウト明確な、しかし、これらのピンを表示するビューコントローラ上のマップビューを変更しないでください。アノテーションをマップから削除するには、removeAnnotationsを呼び出す必要があります。

あなたの注釈が削除されたことを検出viewWillAppearメソッドを追加し、それらを削除することができます。このような何か:

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

上記のコードは、全体の注釈辞書をuserDefaultsから削除された場合のみ、マップから注釈を削除するに書き込まれます。そうすれば、ビューコントローラがフォーカスを取り戻すたびにアノテーションを再ロードすることを避けることができます。

ところで、あなたはあなたの新しいシーンが呼び出されたと述べ、「PopoverOptions。」シーンがポップオーバーで提示された場合、上記は機能しない可能性があります。 (私はviewWillAppearがポップオーバーが却下されたビューコントローラに呼び出されるとは思わないが、私は確かに覚えていません。)

+0

おかげでたくさんのお返事を、あなたは私にこの例を与えることができますか?私はスイフトに新しいです:) – Oscar

+0

もう一度ありがとうございます。どのクラスをコードに入れればいいですか? – Oscar

+0

まあ、私が言ったように、私はスウィフトには新しいです。だから明らかに学習は私の主な目標です。結果にかかわらず:)ありがとう、私はちょうど確かめたかったのです。あなたが私に与えたコードはまだ動かない、 "if let"行にエラーが出る – Oscar

関連する問題