Add Custom Property to MKAnnotation

廉价感情. 提交于 2019-12-21 23:54:17

问题


I am trying to add a custom value to my MKAnnotation. I would like to have it store the unique ID of the location. My code for setting up the annotation with a title and subtitle works like this:

location.latitude = [dictionary[@"placeLatitude"] doubleValue];
location.longitude = [dictionary[@"placeLongitude"] doubleValue];

newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"placeName"]
                                               andCoordinate:location];

newAnnotation.subtitle = dictionary[@"placeCity"];

How would I add a custom property, for example "placeId"? this is what I have:

newAnnotation.placeId=dictionary[@"placeId"];

Any help would be great. Thank you!


回答1:


If MapViewAnnotation is subclass of MKAnnotation/MKAnnotationView then simply create property for it, synthesize and use. And if not then create category over MKAnnotationView.

Here are the steps to create category class.

  1. Right click on project and select "New File"
  2. Cacoa Touch > Objective-C category > Next
  3. Give Category : category_name & Category on : category_on_class
  4. in .h file just create property of placeId
  5. in .m synthesize it using @dynamic
  6. import that .h file in you file.

Then you can have "classObject.placeId" property on "classObject" object of that class.




回答2:


Do what Bhargavi said, but do it to MapViewAnnotation.m and MapViewAnnotation.h



来源:https://stackoverflow.com/questions/14848404/add-custom-property-to-mkannotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!