2
votes

So I have create a class X as follows:

@interface X : NSObject <MKAnnotation> {
    CLLocationCoordinate2D  coordinate;
    NSString * title;
    NSString * subtitle;
    UIImage * image;
    NSInteger * tag;
}

@property (nonatomic, readonly) CLLocationCoordinate2D  coordinate;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * subtitle;
@property (nonatomic, retain) UIImage * image;
@property (nonatomic, readwrite) NSInteger * tag;

@end

Inside the:

  • (void) mapView: (MKMapView *) mapView annotationView:(MKAnnotationView *) view calloutAccessoryControlTapped:(UIControl *) control

I would like to be able to access the tag property that X has. How is this possible? Can I do [control tag]? How is this suppose to work?

1

1 Answers

1
votes

For the second part, the reason for the warning is that you assign plain integer to NSInteger pointer. NSInteger is of a type int or long

So you are doing (incorrectly):

NSInteger * tag = 2;

EDIT:

This is how you could use NSInteger:

NSInteger myi = 42;
NSLog(@"int: %d", myi);

NSInteger * i = &myi;    // i is a pointer to integer here
*i = 43;                 // dereference the pointer to change 
                         // the value at that address in memory
NSLog(@"int: %d", myi);

Given above, you are trying:

NSInteger * i = &myi;
i = 2;                  // INCORRECT: i is an pointer to integer

Declare tag as NSInteger instead of NSInteger* and use assign in property (I'd give you exact code but I'm on linux atm ...).

END OF EDIT

For the first part I'm not sure how is the object of X being passed to your method, but you should be then able to do [yourobject tag] if the tag method is part of the interface that the method is using to get data from object X.

What I mean is that the MKAnnotation protocol does not have tag property so you have to typecast the object to your type of object e.g. X *anX = (X*)self.annotation; ,or wherever the annotation object comes from, then you should be able to access tag, [anX tag] - if that is the your X object

I've found this example code in Apple docs that uses custom annotation.

In the example the annotation is set on view.

When view is drawn it uses data from the object that implements annotation protocol. The object is typecasted to the actual object before values are accessed (see drawing method of the view).

You can see in the controller how new annotations are set in regionDidChangeAnimated on the view.