5

我想向 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

.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

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

  pinLocation.latitude = 4.8874;
    pinLocation.longitude = 1.400;
    POI *poi = [[POI alloc] initWithCoordinate:pinLocation title:@"foo" andSubTitle:@"bar"];
    [_mapView addAnnotation:poi];
4

2 回答 2

12

三步。

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

@interface ViewController : UIViewController <MKMapViewDelegate> { ... } 

2)将您的视图控制器设置MKMapViewDelegate为接收委托回调的委托。通常在viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.mapView.delegate = self;
}

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

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

- (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];
}
于 2012-12-26T22:35:02.917 回答
0

在你的- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation方法中使用这个:

annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

annotationView要返回的视图在哪里。

于 2012-12-26T22:27:44.120 回答