ios - How to retrieve instance of NSDictionary when AnnotationCallout is tapped? Swift -
my app uses yelp api generate json data store nsdictionary
. user clicks on categories , mapview populates pins information yelp api client. when user taps annotation callout, want display detail view controller contents of dictionary of data yelp client. want instance of array of businesses can display contents on detail view controller.
here code:
this business model query businesses:
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 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) } }
and here delegate method 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); } }
here link repo: https://github.com/ssharif6/park-n-go
again, structure of yelp data:
there array holding different nsdictionaries of business. want retrieve correct instance of dictionary when callout accessory view tapped.
thanks!
mkannotation
protocol. can create custom object want conforms protocol. suggest ditching mkpointannotation
object , creating custom object conforms mkannotation
protocol, , either stores dictionary of information or has properties values need. when user taps on annotation view annotation object contains information need.
alternately create custom subclass of mkpointannotation
contains integer index array of data.
edit:
in code, have following:
var annotation = mkpointannotation()
instead of doing that, create custom class conforms mkannoation protocol:
class myannotation: mkannotation { //define coordinate title (and optionally subtitle) //properties mkannotation protocol var annotationdictionary: dictionary }
then
var annotation = myannotation()
set coordinate, title, , subtitle you're doing, , set dictionary property data need. add myannotation object map doing now.
Comments
Post a Comment