开发者

how to perform event specific to a certain MKAnnotation

i have an map that has a few hundred MKPointAnnotations and have been set up to have a left and right callout accessory, the issue i am having is finding a way to perform an action that is specific to that annotation

for example if someone presses a specific annotation i want to go to a new viewController that has deta开发者_StackOverflow中文版iled information about were the annotation is. I have setup a custom object that conforms to the MKAnnotation so all the data is contained into the annotation as such...

    @synthesize coordinate;
@synthesize _title;
@synthesize _date;
@synthesize _description;
@synthesize _coordinates;

- (CLLocationCoordinate2D)coordinate;{
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = _coordinates.latitude;
    theCoordinate.longitude = _coordinates.longitude;
    return theCoordinate; 
}

- (NSString *)title {
 return _title;
}

- (NSString *)subtitle {
 return _description;
}

- (void)dealloc {
 [super dealloc];
 [_description release];
 [_date release];
 [_title release];
}

can anyone help me with this please :D


Let us consider the class name your code given above is annotation.m

In your viewController

annotation  *annotationObject=[[annotation alloc]init];
annotationObject.title=@"Some title";
annotationObject.subTitle=@"Some subtitle";
annotationObject.coordinate=someCoordinate;
[mapView addAnnotation:annotationObject];
[annotationObject release];

Put the above code in a loop to add many annotations, or put the annotaion objects in a array and use addAnnotations to mapView.

In viewForAnnotation add a accessory button to the annotation

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
 MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"] autorelease];
    annotationView.canShowCallout = YES;
    UIButton *annotationButton=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    annotationView.rightCalloutAccessoryView = annotationButton;
    return annotationView;
}

When user selects the acessory button this delegate will be called

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
     //Get the specific annotation by the view.annotation.title 
     //Go to another view that has details
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜