在 segue 回到原始地图后重置注释图钉 (Mapkit Swift)

Annotation pins reset after segue back to original map (Mapkit Swift)

我正在使用 MapKit 的本地搜索功能来根据用户的搜索填充一堆图钉注释。当您点击图钉时,您会在标注中转到该位置的详细信息 viewcontroller。当用户从 detailviewcontroller 转到上一个 viewcontroller 并单击工具栏上的后退按钮时,所有注释图钉都消失了。有什么办法可以避免这种情况吗?

这是我的本地搜索代码:

@IBAction func returnText(sender: AnyObject) {
    sender.resignFirstResponder();
    attractionsMap.removeAnnotations(attractionsMap.annotations);
    performSearch();
}
func performSearch() {


    matchingItems.removeAll()

    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = searchText.text;

    request.region = attractionsMap.region;

    let search = MKLocalSearch(request: request)

    search.startWithCompletionHandler({(response:
        MKLocalSearchResponse!,
        error: NSError!) in

        if error != nil {
            println("Error occured in search: \(error.localizedDescription)")
        } else if response.mapItems.count == 0 {
            println("No matches found")
        } else {
            println("Matches found")

            for item in response.mapItems as! [MKMapItem] {
                println("Name = \(item.name)")
                println("Phone = \(item.phoneNumber)")

                matchingItems.append(item as MKMapItem)
                println("Matching items = \(matchingItems.count)")

                var placemark = item.placemark;
                var subThoroughfare:String = "";
                var thoroughfare:String = "";
                var locality:String = "";
                var postalCode:String = "";
                var administrativeArea:String = "";
                var country:String = "";
                var title = "";
                var subtitle = "";

                if (placemark.subThoroughfare != nil) {
                    subThoroughfare = placemark.subThoroughfare;
                }
                if(placemark.thoroughfare != nil) {
                    thoroughfare = placemark.thoroughfare;
                }
                if(placemark.locality != nil) {
                    locality = placemark.locality;
                }
                if(placemark.postalCode != nil) {
                    postalCode = placemark.postalCode;
                }
                if(placemark.administrativeArea != nil) {
                    administrativeArea = placemark.administrativeArea;
                }
                if(placemark.country != nil) {
                    country = placemark.country;
                }
                println("viewcontroller placmark data:");
                println(locality);
                println(postalCode);
                println(administrativeArea);
                println(country);

                title = " \(subThoroughfare) \(thoroughfare) \n \(locality), \(administrativeArea) \n \(postalCode)\(country)";
                subtitle = " \(subThoroughfare) \(thoroughfare)";
                println(title);

                var annotation = MKPointAnnotation()
                annotation.coordinate = item.placemark.coordinate
                annotation.title = item.name + " " + subtitle;
                self.attractionsMap.addAnnotation(annotation)
            }
        }
    })
}

然后我通过这个方法传递匹配项的地标...

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var attractionsDetailViewController:AttractionsDetailViewController = segue.destinationViewController as! AttractionsDetailViewController
    attractionsDetailViewController.attractionLocation = indicatedMapItem;
}

任何帮助将不胜感激。

您可以使用 NSUserDefaults、"used to determine an application’s default state"。看看:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/