如何在 Objective-C 中为 pin mapview 中的文本添加事件处理程序?
How to add event handler for the text in pin mapview in Objective-C?
我是第一次尝试使用地图视图功能。我有一个地图视图、文本字段和搜索按钮。目前一切正常:
- (IBAction)searchLocation:(id)sender {
// Create and initialize a search request object.
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = _nameLocation.text;
request.region = _mapview.region;
// Create and initialize a search object.
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
// Start the search and display the results as annotations on the map.
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error)
{
NSMutableArray *placemarks = [NSMutableArray array];
for (MKMapItem *item in response.mapItems) {
[placemarks addObject:item.placemark];
}
[_mapview removeAnnotations:[_mapview annotations]];
[_mapview showAnnotations:placemarks animated:NO];
}];
}
现在我需要向 pin 添加事件处理程序。如果用户单击引脚,我想打开新控制器并将地址显示为文本。如果有人能告诉我如何让听众知道当用户单击图钉时它会为我执行某些功能,我将不胜感激。此外,我想访问显示在 pin 上的地址。
编辑
我实际上需要知道用户何时单击图钉显示的文本。这是 "Miranda NSW" 当用户点击文本时表示他们接受了地址。
这 2 个委托方法会让您知道何时用户 select 和 deselect 引脚
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
像这样在您的 viewForAnnotation
中添加披露按钮
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = rightButton;
现在,当您点击 mapView 上的图钉时,显示的视图中会显示一个公开按钮。然后,您将需要使用以下方法来告诉应用程序在按下披露按钮时要做什么。
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
我是第一次尝试使用地图视图功能。我有一个地图视图、文本字段和搜索按钮。目前一切正常:
- (IBAction)searchLocation:(id)sender {
// Create and initialize a search request object.
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = _nameLocation.text;
request.region = _mapview.region;
// Create and initialize a search object.
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
// Start the search and display the results as annotations on the map.
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error)
{
NSMutableArray *placemarks = [NSMutableArray array];
for (MKMapItem *item in response.mapItems) {
[placemarks addObject:item.placemark];
}
[_mapview removeAnnotations:[_mapview annotations]];
[_mapview showAnnotations:placemarks animated:NO];
}];
}
现在我需要向 pin 添加事件处理程序。如果用户单击引脚,我想打开新控制器并将地址显示为文本。如果有人能告诉我如何让听众知道当用户单击图钉时它会为我执行某些功能,我将不胜感激。此外,我想访问显示在 pin 上的地址。
编辑 我实际上需要知道用户何时单击图钉显示的文本。这是 "Miranda NSW" 当用户点击文本时表示他们接受了地址。
这 2 个委托方法会让您知道何时用户 select 和 deselect 引脚
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
像这样在您的 viewForAnnotation
中添加披露按钮
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = rightButton;
现在,当您点击 mapView 上的图钉时,显示的视图中会显示一个公开按钮。然后,您将需要使用以下方法来告诉应用程序在按下披露按钮时要做什么。
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control