问题
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.
- Right click on project and select "New File"
- Cacoa Touch > Objective-C category > Next
- Give Category : category_name & Category on : category_on_class
- in .h file just create property of placeId
- in .m synthesize it using @dynamic
- 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