访问 alertView 的调用视图 [英] access an alertView's calling view
问题描述
我有一个 UIAlertView,当用户想要删除 RegionAnnotation 时,它会显示为确认.
I have a UIAlertView which appears as a confirmation when a user wants to delete a RegionAnnotation.
我在弄清楚如何访问调用 UIAlertView 的 RegionAnnotationView 时遇到了问题,我需要它来删除 RegionAnnotation.
I'm having trouble figuring out how to access the RegionAnnotationView that called the UIAlertView which I need in order to delete the RegionAnnotation.
这是我损坏的代码 - 您可以看到我试图将 AlertView 的超级视图转换为 RegionAnnotationView 的位置(这是一个公认的坏主意).
Here's my broken code - you can see where I'm trying to cast the AlertView's superview into a RegionAnnotationView (an admittedly bad idea).
- (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];
}
}
推荐答案
由于注解在用户删除之前被选中,因此您可以从地图视图的 selectedAnnotations
属性中获取对该注解的引用.
Since the annotation is selected before the user can delete it, you can get a reference to the annotation from the map view's selectedAnnotations
property.
在警报视图委托方法中,您可以执行以下操作:
In the alert view delegate method, you can do something like this:
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...
}
如果注释未被选中,另一种简单的替代方法是使用 ivar 来保存对需要删除的注释的引用.
If the annotation wasn't selected, another simple alternative is to use an ivar to hold a reference to the annotation that needs to be deleted.
MSK 评论的另一个选项是使用 objc_setAssociatedObject.
Another option as MSK commented is to use objc_setAssociatedObject.
无论如何,假设视图层次结构以某种方式使用超级视图并不是一个好主意.
Regardless, using the superview assuming the view hierarchy is in a certain way is not a good idea.
这篇关于访问 alertView 的调用视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!