objective-c - 如何向 MKAnnotation 添加披露按钮?

标签 objective-c android-mapview mapkit mkannotation disclosure

我想向 MKAnnotation 添加一个披露按钮以转到另一个 View 。

按钮应该是这样的:

Image

这是我的.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];

最佳答案

三个步骤。

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

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

2) 将您的 View Controller 设置为 MKMapViewDelegate 的委托(delegate)以接收委托(delegate)回调。通常在 viewDidLoad 中完成:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.mapView.delegate = self;
}

3) 实现以下委托(delegate)函数以显示披露按钮:

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

以下功能将有助于确定在触摸披露按钮后采取什么行动(在您的情况下,呈现 View )。

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

关于objective-c - 如何向 MKAnnotation 添加披露按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14046972/

相关文章:

android - 如何使用 XYTileSource 设置 osmdroid 源

objective-c - 类别冲突

objective-c - 无法弄清楚这个动态类型的事情

除非您触摸 map View ,否则不会加载 Android MapView

swift - map 按钮刷新位置

swift - 在 ViewModel 中链接可观察对象以获取但作为独立属性保留

swift - MKPolyLine swift 4

objective-c - iOS核心数据: When is data recreatable?

objective-c - Objective C unicode 字符比较

Android:无法从 View 转换到 MapView