add id to pin annotation in objective c to load de

2019-07-30 19:09发布

I have a mapview controller which shows pins from an array of business objects I have created (each business has a method to get the title and subtitle of the pin).

I have added a disclosure button to each pin annotation which works correctly, but I am not sure how to pass a variable to the detail view which is to be loaded from the disclosure button, and show all the details for that particular business.

I add my businesses to the array like this (in viewWillAppear)...

// fetch model data for table view
SGTGAppDelegate *appDelegate = (SGTGAppDelegate *)[[UIApplication sharedApplication] delegate];

self.businesses = appDelegate.vaBusinesses;

// Add the business to the map
[self.mapView addAnnotations:self.businesses];

I then format the annotations like this...

-(MKAnnotationView *)mapView:(MKMapView *)amapView viewForAnnotation:(id<MKAnnotation>)annotation{
    static NSString *PinIdentifier = @"PinIdentifier";

    //Use default style for user location
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    //Obtain a pin
    MKPinAnnotationView *pin = (MKPinAnnotationView *) [amapView dequeueReusableAnnotationViewWithIdentifier:PinIdentifier];

    if (pin == nil){
        pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:PinIdentifier] autorelease];
    }

    UIButton * detailView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];


    // Configue the pin
    pin.annotation = annotation;
    pin.animatesDrop = NO;
    pin.pinColor = MKPinAnnotationColorRed;
    pin.rightCalloutAccessoryView = detailView;
    pin.canShowCallout = YES;

    return pin;
}

I then have this method to handle the disclosure button but not sure what do here to get an id of the business to pass onto the detail view...

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"annotation %@", view.annotation[0]);
    // Fetch the businesses for this row

    // not sure what to do here
    //SGTGVABusiness *business = [self.businesses objectAtIndex:[view.annotation]];

    // Show the detail view by pushing it onto the navigation stack
    SGTGVADetailViewController *dvc = [[SGTGVADetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
    //dvc.business = business;
    [self.navigationController pushViewController:dvc animated:YES];
    [dvc release];

}

2条回答
做个烂人
2楼-- · 2019-07-30 19:53

What really needs customization here is the annotation. You're not having any trouble getting from the annotation view to the annotation; the problem is that the annotation is uninformative. What you want to do is create your own annotation class, an NSObject subclass that implements the MKAnnotation protocol, like this:

@interface MyAnnotation : NSObject <MKAnnotation>
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title, *subtitle;
- (id)initWithLocation:(CLLocationCoordinate2D)coord;
@end

@implementation MyAnnotation
- (id)initWithLocation: (CLLocationCoordinate2D) coord {
    self = [super init];
    if (self) {
        self->_coordinate = coord;
    }
    return self;
}
@end

That's minimal, but now you can expand on it. In particular, you can add another property that stores extra info about this annotation. When you create the annotation and add it to the map, you create an instance of this class and assign it the info you will need to fetch later.

My book discusses this in depth:

http://www.apeth.com/iOSBook/ch34.html#_annotations

And you can download a working project that develops this notion:

https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/ch34p848map/p707p723map

查看更多
相关推荐>>
3楼-- · 2019-07-30 20:00

You can subclass MKAnnotationView and create a property which will hold the ID.

查看更多
登录 后发表回答