2011-10-14 1 views
0

MapView에서 작업 중이며 30 개의 핀 위치가 있으며 모든 것이 잘 작동합니다. 콜 아웃에 rightCalloutAccessoryView 버튼을 추가했습니다. 버튼 코드는 다음과 같습니다.지도에서 다른 설명 선보기를 만들기 위해 주석 배열 만들기

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    pinView.rightCalloutAccessoryView = rightButton; 
    [rightButton addTarget:self action:@selector(rightButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    return pinView; 
} 

이 코드는 올바르게 작동하며 "rightButtonPressed"메소드를 호출합니다. 여기에 그 방법의 구현이다 : 그래서

-(IBAction) rightButtonPressed:(id)sender 
{ 
    NSLog(@"button clicked"); 
    MapViewController *thisMap = (MapViewController *)[[UIApplication sharedApplication] delegate]; 
    DetailViewController *dvc = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil]; 
    [thisMap switchViews:self.view toView:dvc.view]; 
} 

, 당신이보고, 나는 모든 30 핀 콜 아웃에서 모든 버튼을 터치 할 때마다, 동일한 뷰 (DetailViewController)로 이동합니다. 각 버튼에 전환 할 자신의보기가 있어야합니다 (여기에서 '길 찾기'및 '길 찾기'등은 물론 상점 주소 및 이름).

30 개의 뷰를 만들고 각 핀에 적용 할 수있는 30 가지 방법을 만들 수 있지만 배열을 다루는보다 간결한 코드가 있어야한다는 것을 알고 있습니다.

DetailViewController의 UILabel에서 특정 배열을 호출 할 수 있으므로 해당 정보를로드하고 적절한 위치로 방향을 지정합니다.

이것은 큰 질문 일 수 있으며 일부 자습서를 둘러 보았지만 질문에 정확히 답하는 항목을 찾지 못했습니다. 누구든지 나를 시작하게 할 수 있다면 (또는 심지어 올바른 방향으로 나를 가리키고), 나는 완전히 감사 할 것입니다.

답변

1

주석보기 설명 선 액세서리로 addTarget을 사용하는 대신 버튼보기를 처리하려면지도보기 자체의 대리자 방법 calloutAccessoryControlTapped을 사용하는 것이 훨씬 더 좋습니다.

calloutAccessoryControlTapped 대리자 메서드에서 배열의 어떤 주석이나 탭 된 데이터 구조를 알아낼 필요없이 view.annotation을 사용하여 탭한 주석에 직접 액세스 할 수 있습니다.

addTarget에 호출을 제거하고 함께 rightButtonPressed: 방법을 대체 :

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
      calloutAccessoryControlTapped:(UIControl *)control 
{ 
    //cast the plain view.annotation to your custom class so you can 
    //easily access your custom annotation class' properties... 
    YourAnnotationClass *annotationTapped = (YourAnnotationClass *)view.annotation; 

    NSLog(@"button clicked on annotation %@", annotationTapped); 

    MapViewController *thisMap = (MapViewController *)[[UIApplication sharedApplication] delegate]; 
    DetailViewController *dvc = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil]; 

    //annotationTapped can be passed to the DetailViewController 
    //or just the properties needed can be passed... 
    //Example line below assumes you add a annotation property to DetailViewController. 
    dvc.annotation = annotationTapped; 

    [thisMap switchViews:self.view toView:dvc.view]; 
} 
+0

좋아! 생각보다 훨씬 쉬워졌습니다 (여전히지도 뷰 대리자의 방법에 익숙해 져 있습니다). 내가 지금 가지고있는 한 가지 문제는 (이전의 방법에서도 마찬가지였다) 프로그램과 모든 것이 잘 작동하지만 'thisMap switchViews : self.view toView : dvc.view];' 그것은 나에게 "MapViewController"가 'switchViews : toView :'에 응답하지 않을 수도 있다는 경고를줍니다. 나중에 문제가 될 경우에 대비하여 지금 바로 정리하고 싶습니다. 정말 고마워! – MillerMedia

+0

MapViewController.h (인터페이스) 파일에서 switchViews 메소드를 선언했는지 확인하십시오. – Anna

+0

switchViews 메서드가 '- (void) mapView'메서드 내에서 호출 되었기 때문에 혼란 스럽습니다. 선언 할 구문은 무엇입니까? 나는 그것의 방법을 선언하는 법을 안다. 그러나 이것과 같은 것은 어떨까요? (미안 해요! 아직도 배우고 있어요 ...) – MillerMedia

관련 문제