我想向 MKAnnotation
添加一个披露按钮以转到另一个 View 。
按钮应该是这样的:
这是我的.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/