如何向MKAnnotation添加公开按钮?

fnt*_*lnz 5 objective-c disclosure mapkit mkannotation android-mapview

我想在a中添加一个公开按钮以MKAnnotation转到另一个视图.

按钮应如下所示:

图片

这是我的.h.m文件.


.h文件

//
//  POI.h
//

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface POI : NSObject <MKAnnotation> {

    NSString *title;
    NSString *subtitle;
    CLLocationCoordinate2D coordinate;
}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

- (id)initWithCoordinate:(CLLocationCoordinate2D)_coordinate title:(NSString *)_titolo andSubTitle:(NSString *)_sottotitolo;


@end
Run Code Online (Sandbox Code Playgroud)

.m文件

//
//  POI.m

#import "POI.h"



@implementation POI

@synthesize title, subtitle, coordinate;
-(id)initWithCoordinate:(CLLocationCoordinate2D)_coordinate title:(NSString *)_titolo andSubTitle:(NSString *)_sottotitolo {

    [self setTitle:_titolo];
    [self setSubtitle:_sottotitolo];
    [self setCoordinate:_coordinate];



    return self;
}

@end
Run Code Online (Sandbox Code Playgroud)

在我的ViewController中,我使用以下方法调用:

  pinLocation.latitude = 4.8874;
    pinLocation.longitude = 1.400;
    POI *poi = [[POI alloc] initWithCoordinate:pinLocation title:@"foo" andSubTitle:@"bar"];
    [_mapView addAnnotation:poi];
Run Code Online (Sandbox Code Playgroud)

pal*_*lmi 12

三个步骤.

1)在头文件(.h)实现文件的(.m)类扩展符合MKMapViewDelegate:

@interface ViewController : UIViewController <MKMapViewDelegate> { ... } 
Run Code Online (Sandbox Code Playgroud)

2)将视图控制器设置MKMapViewDelegate为接收委托回调的委托.常用于viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.mapView.delegate = self;
}
Run Code Online (Sandbox Code Playgroud)

3)实现以下委托功能以显示披露按钮:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{   
    MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc]     initWithAnnotation:annotation reuseIdentifier:@"pinLocation"];

    newAnnotation.canShowCallout = YES;
    newAnnotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    return newAnnotation;
}
Run Code Online (Sandbox Code Playgroud)

以下功能将有助于确定在触摸公开按钮时采取的操作(在您的情况下,呈现视图).

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    //launch a new view upon touching the disclosure indicator
    TestVCViewController *tvc = [[TestVCViewController alloc] initWithNibName:@"TestVCViewController" bundle:nil];
    [self presentViewController:tvc animated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud)