【问题标题】:Pass information from UIAlertView linked to an annotation从链接到注释的 UIAlertView 传递信息
【发布时间】:2013-01-01 01:51:37
【问题描述】:

我是 Xcode 的新手,所以如果我的问题很愚蠢,请原谅我的问题

我有一个链接到 UIAlertView 框的注释,它有两个选择(关闭,到这里的方向) 第二个按钮应该打开苹果地图应用程序,并立即为用户提供转弯导航。

现在我的问题是我的地图上有很多注释,当按下每个注释时,用户必须获得导航选项。所以我没有固定的 MKPlacemark,我需要将信息从按下的注释传递给 MKPlacemark,以便 MKMapItem 获得所需的标题位置。

我的代码是:

我的 Annotation 方法将显示一个 UIAlertView:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
// get reference to the annotation to access its data
VBAnnotation *ann = (VBAnnotation *)view.annotation;
// deselect the button
[self.mapView deselectAnnotation:ann animated:YES];

// display alert view to the information
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ann.title message:ann.info delegate:self cancelButtonTitle:@"close" otherButtonTitles:@"direction to here", nil];
[alert show];
}

这是我的 UIAlertView 按钮操作:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"direction to here"])
{        

//now here i tried a fixed coordination, but i need to pass the actual coordination pressed by the UIAlertView
CLLocationCoordinate2D coordinate;
    coordinate.latitude = 24.41351;
    coordinate.longitude = 39.543002;

    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
    MKMapItem *navigation = [[MKMapItem alloc]initWithPlacemark:placemark];
    NSDictionary *options = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeWalking};
    [navigation openInMapsWithLaunchOptions:options];

}
}

这是我的注释列表示例

NSMutableArray *annotations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
VBAnnotation *ann;


//Annotations List

// Mecca Pin
location.latitude = MEC_LATITUDE;
location.longitude = MEC_LONGITUDE;
ann = [[VBAnnotation alloc] init];
[ann setCoordinate:location];
ann.title = @"NewHorizons Institute";
ann.subtitle = @"English and computer training center";
[annotations addObject:ann];
[self.mapView addAnnotations:annotations];

当然,我有一个名为 VBAnnotation 的类

谢谢...

【问题讨论】:

    标签: objective-c xcode mapkit


    【解决方案1】:

    如果我理解正确,您需要记住 VBAnnotation 对象,以便警报视图委托方法可以访问它。您可以在显示警报视图之前使用objc_setAssociatedObject() 将对象与警报视图关联,然后在处理用户对警报的响应时使用objc_getAssociatedObject() 检索对象。

    This article 有更多关于 Obj-C 关联对象的信息。要导入以获取功能的文件是这样的:

    #import <objc/runtime.h>
    

    如果VBAnnotation 对象可以用某种数字 ID 或索引来标识,则更简单的方法是将该 ID 或索引存储在警报视图的 tag 属性中 - UIAlertView 是一个子类的UIView 并因此继承了该属性。

    【讨论】:

    • 谢谢...我的 VBAnnotation 对象拥有 5 个属性: // Coordinate //title //subTitle //informations //name 但我唯一需要传递的是协调...谢谢。 ..这篇文章真的很有帮助
    • @AliRaslan 如果这个答案有助于解决您的问题,如果您接受它会很好。否则,您可以编写自己的答案并接受。
    • 对不起 :$ 我是该网站的新手,不知道接受按钮 :)
    猜你喜欢
    • 1970-01-01
    • 2020-03-04
    • 2018-03-11
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多