2013-01-01 75 views
1

我是新来的Xcode所以请原谅我的问题,如果它是愚蠢的传递信息链接到注释

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

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

我的代码是:

我的注释方法,它会显示一个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

感谢类...

回答

2

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

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

#import <objc/runtime.h> 

如果VBAnnotation对象可以用某种形式的数字ID或索引标识,更简单的方法是将存储在tag该ID或索引警报视图的属性 - UIAlertViewUIView的子类,因此继承该属性。

+0

谢谢...我VBAnnotation对象包含5个属性: //坐标 //标题 //字幕 //信息 //名 但我需要传递的唯一事情是协调...... 谢谢...这篇文章真的很有帮助 –

+0

@AliRaslan如果这个答案有助于解决你的问题,那么如果你接受了它,那将会很好。否则,您可以编写自己的答案并接受该答案。 – herzbube

+0

对不起:$我是新来的网站,并不知道接受按钮:) –