Objective c 将id添加到目标c中的pin注释以加载detailview
我有一个mapview控制器,它显示我创建的一系列业务对象中的pin(每个业务都有一个方法来获取pin的标题和副标题) 我已经为每个pin注释添加了一个正常工作的披露按钮,但我不确定如何将变量传递到要从披露按钮加载的详细视图,并显示特定业务的所有详细信息 我将我的企业添加到数组中,如下所示(在ViewWillDisplay中) 然后我像这样格式化注释Objective c 将id添加到目标c中的pin注释以加载detailview,objective-c,mapkit,mkannotation,detailview,Objective C,Mapkit,Mkannotation,Detailview,我有一个mapview控制器,它显示我创建的一系列业务对象中的pin(每个业务都有一个方法来获取pin的标题和副标题) 我已经为每个pin注释添加了一个正常工作的披露按钮,但我不确定如何将变量传递到要从披露按钮加载的详细视图,并显示特定业务的所有详细信息 我将我的企业添加到数组中,如下所示(在ViewWillDisplay中) 然后我像这样格式化注释 -(MKAnnotationView *)mapView:(MKMapView *)amapView viewForAnnotation:(id&
-(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;
}
您可以将MKAnnotationView子类化,并创建一个保存ID的属性。这里真正需要定制的是注释。从注释视图到注释没有任何问题;问题是注释没有信息性。您要做的是创建自己的annotation类,一个实现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
@接口MyAnnotation:NSObject
@性质(非原子)CLLocationCoordinate2D坐标;
@属性(非原子,副本)NSString*标题,*副标题;
-(id)initWithLocation:(CLLocationCoordinate2D)coord;
@结束
@实现MyAnnotation
-(id)initWithLocation:(CLLocationCoordinate2D)coord{
self=[super init];
如果(自我){
自坐标=坐标;
}
回归自我;
}
@结束
这是最小的,但是现在你可以扩展它了。特别是,您可以添加另一个属性来存储有关此批注的额外信息。创建注释并将其添加到地图时,将创建此类的实例,并为其分配稍后需要获取的信息
我的书对此进行了深入讨论:
您可以下载一个开发此概念的工作项目:
-(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];
}
@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