1

서비스를 호출하고 MapKit을 사용하여지도에 배치 할 위도와 경도를 반환합니다.MKAnnotationView RightCallOut 버튼을 클릭하면 앱이 깨집니다.

MKAnnotationView 사용 각 주석에 RightCallOutButton을 추가하고 있습니다.

그래서 새로운 MapDelegate를 만들어야했습니다. 아래 코드.

버튼을 클릭하면 앱이 다운되고 MonoTouch에서 선택기가 이미 GC (가비지 수집) 된 액티비티 무언가라고 말하는 오류가 발생합니다.

그럼 내 질문에, RightCalloutAccessoryView를 어디에 설정해야할까요? 아래 코드가 아니라면 어디서 버튼을 만들어야합니까?

public class MapDelegage : MKMapViewDelegate { 

    protected string _annotationIdentifier = "BasicAnnotation"; 
    public override MKAnnotationView GetViewForAnnotation (MKMapView mapView,       NSObject annotation) { 

MKAnnotationView annotationView = mapView.DequeueReusableAnnotation(this._annotationIdentifier); 


if(annotationView == null) { 
    annotationView = new MKPinAnnotationView(annotation, this._annotationIdentifier); 
} else { 
    annotationView.Annotation = annotation; 
} 


annotationView.CanShowCallout = true; 
(annotationView as MKPinAnnotationView).AnimatesDrop = true;  
(annotationView as MKPinAnnotationView).PinColor = MKPinAnnotationColor.Green; 
annotationView.Selected = true;  
var button = UIButton.FromType(UIButtonType.DetailDisclosure); 
button.TouchUpInside += (sender, e) => { 
new UIAlertView("Testing", "Testing Message", null, "Close", null).Show(); 
} ; 

annotationView.RightCalloutAccessoryView = button; 
return annotationView; 
} 

} 

답변

1
annotationView = new MKPinAnnotationView(annotation, this._annotationIdentifier); 
... 
var button = UIButton.FromType(UIButtonType.DetailDisclosure); 

당신은 당신이 방법 자체를 오래 살 것으로 예상 참조를 유지하는 지역 변수를 선언하지 않도록해야합니다. annotationView 또는 button에 대한 참조가 없으면 네이티브 대응 항목이 있더라도 가비지 수집기 (GC)는 수집 할 수 있습니다 (관리되는 부분). 그러나 콜백이 호출되면 충돌이 발생합니다.

가장 쉬운 방법은보기를 삭제할 때 목록을 지우고 (클래스 수준에서 List<MKPinAnnotationView> 필드) 목록을 지우는 것입니다. UIButton은 뷰와 뷰 사이에 참조가 있으므로 필요하지 않습니다.

참고 : 작업은 숨기기 MonoTouch의 향후 버전에서 개발자들이 이러한 복잡성으로 수행되고있다. 슬프게도 당신은 지금이 문제를 무시할 수 없습니다.

+0

그래서 MapDelegate 클래스 내에 List <>를 만들겠습니까? 보기를 삭제하면 목록을 지우는 것이 무슨 뜻인지 확실치 않습니다. PInAnnotations를 정확히 어디에 추가합니까? 나는 GetViewForAnnotation이 한 번에 하나의 주석을 처리했다고 생각 했습니까? –

+0

알았어. 이해할 것 같아서 일하게. List를 만들고 annoationView를 목록에 추가했습니다. 내가 만든 List에 대해 걱정할 필요가 있습니까? 아니면 어떤 시점에서 GC에 의해 파괴 될 것입니까? –

+0

GC에 의해 필드의 상위 인스턴스에 대한 참조가 없으면 가능한 일이지만, 커질 수 있으므로 가능한 한 빨리 '부모'가 Dispose 될 수 있도록하는 것이 좋습니다. 보기를 숨기거나 표시하거나 (또는 ​​캐시 된 상태로 유지하고 iOS에서 메모리 부족 경고를받을 때만 지우면) 수동으로 지울 수도 있습니다. – poupou

관련 문제