使用MapKit框架,我想通过在相应的calloutView中使用按钮(“删除”)来删除(自定义)注释。当按下按钮时,错误出现在Xcode的调试部分:
***-[NSButton lockFocus],/ SourceCache / AppKit / AppKit-1348.17 / AppKit.subproj / NSView.m:7629中的断言失败
回溯是: 0 CoreFoundation 0x00007fff8347203c __exceptionPreprocess + 172 1个libobjc.A.dylib 0x00007fff84d6976e objc_exception_throw + 43 2 CoreFoundation 0x00007fff83471e1a + [NSException提高:格式:参数:] + 106 3基础0x00007fff8a61697b-[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195 4 AppKit 0x00007fff8de035c6-[NSView lockFocus] + 233 5 AppKit 0x00007fff8df8b164-[NSControl mouseDown:] + 200
在仍然显示calloutView时不能删除它是很合逻辑的,因此我实现了以下过程:
在按钮的IBAction中,注释被标记为删除,然后将注释设置为取消选择
在地图视图的didDeselect ..委托中,实际删除发生了
-(IBAction)trash:(id)sender {
NSLog(@"put to trash");
MyPointAnnotation *thisAnnotation = (MyPointAnnotation*)[relatedAnnotationView annotation];
[thisAnnotation setFlaggedForDeletion:YES]; // deletion is managed in delegate didDeselect
[thisAnnotation.pointMetadata->mapView deselectAnnotation:thisAnnotation animated:NO];
}
-(void) mapView:(MKMapView *)theMapView didDeselectAnnotationView:(nonnull MKAnnotationView *)view {
if (customCalloutView) {
[customCalloutView removeFromSuperview];
}
if([(MyPointAnnotation*)[view annotation] flaggedForDeletion]){
[view removeFromSuperview];
[theMapView removeAnnotation:[view annotation]];
}
}
我希望不会收到任何警告/错误,因为在按下按钮时不会删除注释。