2012-07-02 2 views
2

저는 XCode 영역에서 새로운 사람으로 누군가가 나를 도울 수 있는지 궁금해하고있었습니다.removeOverlay : 오버레이가 작동하지 않습니다.

본질적으로, 나는 WWDC2010의 TileMap 프로젝트 예제를 가지고 놀고 있는데, 분할 된 컨트롤러를 사용하여 NOAA 차트를 숨기는 방법을 찾으려하고있다.

오버레이를 활성화 할 수는 있지만 잘 표시되지만 세그먼트 된 컨트롤러를 사용하여 제거 할 수는 없습니다. 상관없이 당신 이름 제어 동작 방법에

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSLog(@"initial view loaded"); 
} 

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay { 

    TileOverlayView *view = [[TileOverlayView alloc] initWithOverlay:overlay];  
    view.tileAlpha = 1; 
    return view; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:  (UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

- (IBAction)switchMap:(id)overlay { 

    if (controller.selectedSegmentIndex == 0) { 
     NSLog(@"welp... it loaded..."); 
     [map removeOverlay:overlay]; 
    } 

    if (controller.selectedSegmentIndex == 1) { 

     NSLog(@"Map Overlay works"); 
     NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Tiles"]; 
     TileOverlay *overlay = [[TileOverlay alloc] initWithTileDirectory:tileDirectory]; 
     [map addOverlay:overlay]; 

     MKMapRect visibleRect = [map mapRectThatFits:overlay.boundingMapRect]; 
     visibleRect.size.width /= 2; 
     visibleRect.size.height /= 2; 
     visibleRect.origin.x += visibleRect.size.width/2; 
     visibleRect.origin.y += visibleRect.size.height/2; 
     map.visibleMapRect = visibleRect; 

    } 


    if (controller.selectedSegmentIndex == 2) { 
     NSLog(@"But... overlay isnt hiding waa"); 
     NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Tiles"]; 
     TileOverlay *overlay = [[TileOverlay alloc] initWithTileDirectory:tileDirectory]; 
     [map removeOverlay:overlay]; } 
    } 

답변

1

, 첫 번째 매개 변수 (:

@interface ViewController : UIViewController <MKMapViewDelegate> { 
    IBOutlet MKMapView *map; 
    IBOutlet UISegmentedControl *controller; 
}  
- (IBAction)switchMap:(id)sender; 
@end 

여기가하는 .m에 대한 코드 없습니다 : 여기

헤더 파일에서 일부 코드입니다 it)은 항상 메서드를 호출 한 객체입니다.

여기 컨트롤은 UISegmentedControl이므로 switchMap:에 전달 된 매개 변수는 해당 컨트롤에 대한 참조입니다. .h에서 매개 변수는 sender으로 선언되었지만 .m에는 overlay이라는 이름이 지정되었습니다.

이름에 관계없이 세그먼트 화 된 컨트롤 개체이므로 removeOverlay으로 전달하면 아무런 의미가 없습니다. 그래서이 코드에서


:

if (controller.selectedSegmentIndex == 0) { 
    NSLog(@"welp... it loaded..."); 
    [map removeOverlay:overlay]; 
} 

overlay

는 세그먼트 제어를 가리키는 그래서 removeOverlay 아무것도하지 않습니다. 이 코드에서


:

if (controller.selectedSegmentIndex == 2) { 
    NSLog(@"But... overlay isnt hiding waa"); 
    NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Tiles"]; 
    TileOverlay *overlay = [[TileOverlay alloc] initWithTileDirectory:tileDirectory]; 
    [map removeOverlay:overlay]; } 

당신은 새로운 지역 overlay 객체 (컴파일러는 아마 당신에게 매개 변수를 숨기는 로컬 변수에 대한 경고를주고있다)을 만들 수 있습니다. 이 새 객체는 이미지도에 추가 된 오버레이와 별개입니다. 이 새 인스턴스가지도에 처음 추가되지 않았기 때문에이 새 객체에서 removeOverlay을 호출하면 아무런 효과가 없습니다.


당신이 추가하고 제거하거나지도보기의 overlays 배열에서 찾을 수있는 그 바르를 통과 할 때에 바르 참조를 유지해야 하나, 기존의 오버레이를 제거하십시오. 당신이 이제까지 단 하나의 오버레이를 사용할 경우에

그러나지도보기의 overlays 배열의 첫 번째 개체를 전달할 수 있습니다하거나 removeOverlays (복수)를 호출하고 전체 배열을 전달합니다

if (map.overlays.count > 0) 
    [map removeOverlay:[map.overlays objectAtIndex:0]]; 

//OR... 

if (map.overlays.count > 0) 
    [map removeOverlays:map.overlays]; 
+0

감사 안나, 그 나를 위해 많은 것을 정리했다. 나는이 모든 것에 대해 아직 익숙하지 않으며이 반응은 나의 질문에 대답했을뿐만 아니라 그것으로부터 많은 것을 배웠다. 감사! –

관련 문제