删除覆盖:覆盖不起作用



我是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]; }
     }

在控件操作方法中,第一个参数(无论您如何命名)始终是调用该方法的对象。

在这里,控件是一个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 不会执行任何操作,因为此新实例最初从未添加到映射中。


要移除现有叠加层,您必须在添加叠加层时保留对该叠加层的 ivar 引用,然后传递该 ivar 以将其移除,或者在地图视图的overlays数组中找到该叠加层。

但是,如果您只有一个叠加层,则可以在地图视图的overlays数组中传递第一个对象,或者只调用removeOverlays(复数)并传递整个数组:

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

最新更新