2012-10-11 59 views
1

我正在創建一個MKAnnotationView與詳細信息泄露按鈕。UIButton無法正確加載MKAnnotationView

在mapView:viewForAnnotation:我只是創建一個佔位符按鈕。

// the right accessory view needs to be a disclosure button ready to bring up the photo 
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

在圖形頁面:didSelectAnnotationView:其實我創建要使用(連同有關標籤)按鈕

// create a button for the callout 
UIButton *disclosure    = [self.delegate mapController:self buttonForAnnotation:aView.annotation]; 

NSLog(@"DisclosureButton: %@", disclosure); 

// set the button's target for when it is tapped upon 
[disclosure addTarget:self.delegate action:@selector(presentAnnotationPhoto:) forControlEvents:UIControlEventTouchUpInside]; 

// make the button the right callout accessory view 
aView.rightCalloutAccessoryView = disclosure; 

在日誌中,該按鈕似乎完全實例化,以及設置與正確的標籤。

這是按鈕造物主:

/** 
* returns an button for a specific annotation 
* 
* @param sender    the map controller which is sending this method to us (its' delegate) 
* @param annotation   the annotation we need to create a button for 
*/ 
- (UIButton *)mapController:(MapController *) sender 
     buttonForAnnotation:(id <MKAnnotation>) annotation 
{ 
    // get the annotation as a flickr photo annotation 
    FlickrPhotoAnnotation *fpa = (FlickrPhotoAnnotation *)annotation; 

    // create a disclosure button used for showing photo in callout 
    UIButton *disclosureButton  = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

    // associate the correct photo with the button 
    disclosureButton.tag   = [self.photoList indexOfObject:fpa.photo]; 

    return disclosureButton; 
} 

當我選擇註釋的問題就來了。幾秒鐘後,當選擇註釋並點擊詳細披露按鈕時,不會發生任何事情。但是,在敲幾下註釋並測試按鈕後,它最終按預期工作。

奇怪的延遲是怎麼回事?有時,當按鈕開始工作時,它看起來好像是alpha被設置爲0.0,直到你點擊它並出現。

嚴重的是我遇到的更奇怪的問題之一。

回答

2

在調用didSelectAnnotationView委託方法之前,地圖視圖已經基於註解視圖的屬性(在更改之前)準備了標註視圖。

因此,您在第一次點擊時看到的標註沒有應用程序在didSelectAnnotationView中所做的更改。在以下的水龍頭中,標註可以基於從先前水龍頭設置的值(這實際上取決於在viewForAnnotation中如何處理註解視圖的重用)。

它看起來像代碼在didSelectAnnotationViewbuttonForAnnotation中唯一做的事情是設置按鈕動作和標記。

我假設您使用的是「標籤」方法,因爲presentAnnotationPhoto:方法需要引用選定的註釋屬性。

您不需要使用標籤來獲取操作方法中的選定註釋。取而代之的是,有幾個更好的選擇:

  • 您的自定義操作方法可以從地圖視圖的selectedAnnotations屬性選擇的註釋。有關如何執行此操作的示例,請參閱this question
  • 使用地圖視圖自己的代理方法calloutAccessoryControlTapped而不是自定義操作方法。委託方法傳遞對註釋視圖的引用,該視圖包含指向其註釋的屬性(即view.annotation),因此不會猜測,搜索或詢問選擇了哪個註釋。我推薦這個選項。

在第一個選項,做viewForAnnotationaddTarget和不打擾設置tag。您也不需要buttonForAnnotation方法。然後在按鈕操作方法中,從mapView.selectedAnnotations獲取選定的註釋。

當前,您的操作方法在self.delegate上,因此您可能在訪問來自其他控制器的地圖視圖時遇到一些問題。你可以做的是創建地圖控制器本地按鈕操作方法它獲取所選擇的註釋和然後呼籲self.delegatepresentAnnotationPhoto:操作方法(除了現在可以寫這個方法接受一個註釋參數而不是一個按鈕點擊處理程序)。除非你不需要做任何addTarget並在calloutAccessoryControlTapped方法,調用presentAnnotationPhoto:self.delegate

第二個選項是相似的。

對於這兩個選項中,我建議修改的當前UIButton *和地圖控制器代替presentAnnotationPhoto:方法接受註釋對象本身(FlickrPhotoAnnotation *),做一個addTarget上的方法本地到地圖控制器(或使用calloutAccessoryControlTapped)並從該方法中手動調用presentAnnotationPhoto:並將其傳遞給註釋。

+0

如果你在我面前,我會吻你。非常感謝你抽出時間幫助我解決這個問題。 另外,兩年前,我去看了安娜卡列尼娜,並喜歡它。現在我已經說過了,我希望你沒有選擇那個用戶名,因爲你認爲這個節目很荒謬,哈哈。 再次感謝。 –