我是XCode領域的新人,想知道是否有人能幫助我。removeOverlay:overlay不能正常工作
本質上,我玩弄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]; }
}
感謝安娜,那爲我清理了很多。我對這一切還是陌生的,這個迴應不僅回答了我的問題,而且我也從中學到了很多。謝謝! –