将id添加到目标c中的pin注释以加载detailview

时间:2013-03-21 15:06:39

标签: objective-c mapkit mkannotation detailview

我有一个mapview控制器,它显示我创建的业务对象数组中的引脚(每个业务都有一个方法来获取引脚的标题和副标题)。

我已经为每个引脚注释添加了一个公开按钮,它可以正常工作,但我不知道如何将变量传递给要从公开按钮加载的详细视图,并显示该特定业务的所有详细信息

我将我的商家添加到数组中(在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];

然后我将这样的注释格式化......

-(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;
}

然后我有这个方法来处理披露按钮,但不知道这里做什么来获得传递到详细视图的业务ID ......

-(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 个答案:

答案 0 :(得分:3)

这里真正需要定制的是注释。从注释视图到注释,您没有遇到任何问题;问题是注释没有信息。你想要做的是创建自己的注释类,一个实现MKAnnotation协议的NSObject子类,如下所示:

@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

那是 minimal ,但现在你可以扩展它了。特别是,您可以添加另一个属性来存储有关此批注的额外信息。创建注释并将其添加到地图时,您将创建此类的实例,并为其分配稍后要获取的信息。

我的书深入讨论了这个问题:

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

你可以下载一个开发这个概念的工作项目:

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

答案 1 :(得分:0)

您可以继承MKAnnotationView并创建一个将保存ID的属性。