我的应用程序使用 Yelp API 生成 json 数据,并将其存储到 NSDictionary
中。用户单击类别,然后 map View 会使用来自 Yelp API 客户端的信息填充图钉。当用户点击注释标注时,我想显示一个详细 View Controller ,其中包含来自 Yelp 客户端的数据字典的内容。我想要该企业数组的实例,以便我可以在详细信息 View Controller 上正确显示内容。
这是我的代码:
这是我查询业务的业务模型:
class Resturant: NSObject {
var name: String!
var thumbUrl: String!
var address: String!
var jsonData: NSData!
var location: NSDictionary! // location
init(dictionary: NSDictionary) {
name = dictionary["name"] as? String
thumbUrl = dictionary["thumbUrl"] as? String
address = dictionary["address"] as? String
location = dictionary["location"] as? NSDictionary
}
class func searchWithQuery(map: MKMapView, query: String, completion: ([Resturant]!, NSError!) -> Void) {
YelpClient.sharedInstance.searchWithTerm(query,sort: 0, radius: 1069, success: { (operation: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
let responseInfo = response as! NSDictionary
resultQueryDictionary = responseInfo
println(responseInfo)
let dataArray = responseInfo["businesses"] as! NSArray
for business in dataArray {
let obj = business as! NSDictionary
var yelpBusinessMock: YelpBusiness = YelpBusiness(dictionary: obj)
var annotation = MKPointAnnotation()
annotation.coordinate = yelpBusinessMock.location.coordinate
annotation.title = yelpBusinessMock.name
annotation.subtitle = yelpBusinessMock.displayAddress
map.addAnnotation(annotation)
}
}) { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
println(error)
}
}
这是我的委托(delegate)方法calloutAccessoryViewTapped
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
if (control == view.rightCalloutAccessoryView) {
let selectedLocation = view.annotation;
let selectedCoordinate = view.annotation.coordinate;
var latitude = selectedCoordinate.latitude
var longitude = selectedCoordinate.longitude
var location:CLLocation = CLLocation(latitude: latitude, longitude: longitude)
let businessPlacemark = MKPlacemark(coordinate: selectedCoordinate, addressDictionary: nil)
indicatedMapItem = selectedCoordinate;
let resturantMock:Resturant = Resturant(dictionary: resultQueryDictionary)
attractionDict = resturantMock.location;
performSegueWithIdentifier("attractionToDetail", sender: self);
}
}
这是我的存储库的链接:https://github.com/ssharif6/Park-N-Go
这又是我的 Yelp 数据的结构:
有一个数组保存不同的 Business NSDictionaries。我想在点击标注附件 View 时检索字典的正确实例。
谢谢!
最佳答案
MKAnnotation
是一个协议(protocol)。您可以创建任何符合该协议(protocol)的自定义对象。我建议放弃 MKPointAnnotation 对象并创建一个符合 MKAnnotation 协议(protocol)的自定义对象,并且存储您的信息字典或具有您需要的所有值的属性。然后,当用户点击注释 View 时,注释对象包含您需要的所有信息。
或者,您可以创建 MKPointAnnotation
的自定义子类,其中包含数据数组的整数索引。
编辑:
在您的代码中,您有以下内容:
var annotation = MKPointAnnotation()
不这样做,而是创建一个符合 MKAnnoation 协议(protocol)的自定义类:
class MyAnnotation: MKAnnotation
{
//Define the coordinate title (and optionally subtitle)
//properties from MKAnnotation protocol
var annotationDictionary: Dictionary
}
然后
var annotation = MyAnnotation()
像您正在做的那样设置坐标、标题和副标题,并使用您需要的数据设置字典属性。然后将 MyAnnotation 对象添加到 map 中,就像您现在所做的那样。
关于ios - 点击 AnnotationCallout 时如何检索 NSDictionary 的实例? swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32040851/