2013-01-01 138 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

對不起:$我是新來的網站,並不知道接受按鈕:) –