【发布时间】:2015-03-18 20:30:12
【问题描述】:
我是 swift 新手,目前正试图弄清楚如何获取有关用户选择的注释的数据。我有一个本地搜索功能,可以添加注释,在用户选择一个之后,我希望能够访问它。我正在尝试使用 selectedAnnotations,但它似乎不起作用。
本地搜索:
func performSearch(){
matchingItems.removeAll()
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchTextField.text
request.region = mapView.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)")
self.matchingItems.append(item as MKMapItem)
println("Matching items = \(self.matchingItems.count)")
var annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
annotation.subtitle = item.placemark.title
self.mapView.addAnnotation(annotation)
}
}
})
从那里我正在尝试使用
var selectedAnnotations: [MKPointAnnotation]!
// print signout location
println(selectedAnnotations)
访问注解,但这只是返回“nil”
标注方法:
@IBAction func signoutToLocationButton(sender: AnyObject) {
// saves current user location
PFGeoPoint.geoPointForCurrentLocationInBackground {
(geoPoint: PFGeoPoint!, error: NSError!) -> Void in
if error == nil {
// do something with the new geoPoint
println(geoPoint)
var signoutLocation = PFObject(className: "SignoutLocation")
signoutLocation["Location"] = geoPoint
signoutLocation.saveInBackgroundWithBlock {
(success: Bool, error: NSError!)-> Void in
if (success) {
// has been saved
}
else {
//went wrong
}
}
}
// get location of where they are signing out to
self.mapView.selectedAnnotations(AnyObject)
// print signout location
// println(selectedAnnotations)
}
【问题讨论】:
-
您的代码正在创建一个名为“selectedAnnotations”的局部变量,它是一个空数组。您可能打算访问地图视图的 selectedAnnotations 属性(例如
self.mapView.selectedAnnotations)。另外:你用什么方法检查选择? -
用户选择按钮时调用的不同方法。你想让我也发布那个方法吗?
-
尝试更新代码以使用
self.mapView.selectedAnnotations,如果仍有问题,请发布方法。如果按钮在注解的标注上,更简单的方法是使用 calloutAccessoryControlTapped 委托方法。 -
该按钮不在注释标注上。不过好吧,我会试试的。谢谢!
-
现在告诉我“(AnyObject).Protocol -> $T3' 与 '[AnyObject]' 不同”