add id to pin annotation in objective c to load detailview

六眼飞鱼酱① 提交于 2019-12-24 15:09:42

问题


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];

}

回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/15551202/add-id-to-pin-annotation-in-objective-c-to-load-detailview

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