访问alertView的调用视图

我有一个UIAlertView,当用户想要删除一个RegionAnnotation时,它显示为一个确认。

我很难找出如何访问RegionAnnotationView调用我需要为了删除RegionAnnotation的UIAlertView。

这里是我破碎的代码 – 你可以看到我试图将AlertView的超级视图投射到RegionAnnotationView的位置(一个公认的坏主意)。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if(buttonIndex==0) { NSLog(@"alertView.superview is a %@",alertView.superview); RegionAnnotationView *regionView = (RegionAnnotationView *)alertView.superview; RegionAnnotation *regionAnnotation = (RegionAnnotation *)regionView.annotation; [self.locationManager stopMonitoringForRegion:regionAnnotation.region]; [regionView removeRadiusOverlay]; [self.mapView removeAnnotation:regionAnnotation]; } } 

由于在用户可以删除注释之前select了注释,因此可以从地图视图的selectedAnnotations属性中获取注释的引用。

在警报视图委托方法,你可以做这样的事情:

 if (mapView.selectedAnnotations.count == 0) { //shouldn't happen but just in case } else { //since only one annotation can be selected at a time, //the one selected is at index 0... RegionAnnotation *regionAnnotation = [mapView.selectedAnnotations objectAtIndex:0]; //do something with the annotation... } 

如果没有select注释,另一个简单的select是使用ivar来保存对需要删除的注释的引用。

MSK评论的另一个选项是使用objc_setAssociatedObject。

无论如何,使用superview假设视图层次是以某种方式是不是一个好主意。