【问题标题】:How to add event handler for the text in pin mapview in Objective-C?如何在 Objective-C 中为 pin mapview 中的文本添加事件处理程序?
【发布时间】:2015-08-27 03:30:01
【问题描述】:

我第一次尝试使用地图视图功能。我有一个地图视图、文本字段和搜索按钮。目前一切正常:

- (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 我想打开新控制器并将地址显示为文本。如果有人能告诉我如何让我有一个监听器,当用户单击 pin 时,它会为我执行一些功能,我将不胜感激。此外,我想访问 pin 上显示的地址。

编辑 我实际上需要知道用户何时单击图钉显示的文本。这里是“Miranda NSW” 当用户点击文本时表示他们接受了地址。

【问题讨论】:

    标签: ios objective-c mapkit


    【解决方案1】:

    这 2 个委托方法将让您知道用户何时选择和取消选择引脚

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

    【讨论】:

    • 谢谢。你知道我怎样才能得到图钉中的文字吗?
    • 我没有正确理解你。
    • 是的。当我选择引脚本身时会调用您的代码。但是当用户单击图钉显示的文本时,我需要一个监听器。在上面的例子中是“Miranda NSW, Australia”
    • 现在我唯一的疑问是在 'calloutAccessoryControlTapped' 中如何访问 pin 中的文本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 2016-01-28
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    相关资源
    最近更新 更多