2

私は XCode 領域の新しい人で、誰かが私を助けてくれるかどうか疑問に思っていました。

基本的に、私は WWDC2010 の TileMap プロジェクトの例をいじっており、セグメント化されたコントローラーを使用して NOAA チャートを非表示にする方法を見つけようとしています。

オーバーレイをアクティブにすると問題なく表示されますが、セグメント化されたコントローラーを使用してオーバーレイを削除することはできません。

ヘッダー ファイルのコードを次に示します。

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

.m のコードは次のとおりです。

- (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]; }
     }
4

1 に答える 1

1

コントロールアクションメソッドでは、最初のパラメーター(名前に関係なく)は常にメソッドを呼び出したオブジェクトです。

ここでは、コントロールはUISegmentedControlなので、渡されるパラメーターはswitchMap:そのコントロールへの参照です。.hではパラメータを名前で宣言しましたsenderが、.mでは.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この新しいインスタンスは最初からマップに追加されていないため、この新しいオブジェクトを呼び出しても何も起こりません。


既存のオーバーレイを削除するには、オーバーレイを追加するときにそのオーバーレイへのivar参照を保持し、そのivarを渡して削除するか、マップビューのoverlays配列で見つける必要があります。

ただし、オーバーレイが1つしかない場合は、マップビューの配列の最初のオブジェクトを渡すか、(複数形)をoverlays呼び出して配列全体を渡すことができます。removeOverlays

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

//OR...

if (map.overlays.count > 0)
    [map removeOverlays:map.overlays];
于 2012-07-02T15:21:02.767 に答える