2013-10-09 26 views
1

在我的項目中,我有多個平鋪源添加到地圖中。我有一個按鈕爲每個瓷磚來源。當第一次按下按鈕時,我想讓按鈕添加TileSource,然後下一次removeTileSource並繼續以這種方式交替。我的問題是,它不會刪除TileSource,因爲它刪除了每次按下按鈕時創建的不同myTileSource,因爲我在if語句之前初始化對象。我該如何解決這個問題?我試着在viewDidLoad和if語句中初始化tile源代碼,但是在我調用的其他位置錯誤地使用了「未聲明的標識符」。請仔細閱讀我的代碼,並就如何實現我的預期目標提出建議。謝謝你的時間。如何在IBAction按鈕中初始化平鋪源

- (IBAction)LayerButton:(id)sender 
{ 
    RMMBTilesSource *myTileSource = [[RMMBTilesSource alloc] initWithTileSetURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"MapName" ofType:@"mbtiles"]]]; 
    FirstViewController *FVC = [self.tabBarController.viewControllers objectAtIndex:0]; 
    self.phase3BIsChecked = !self.phase3BIsChecked; 

    if((self.phase3BIsChecked)) { 
     [[FVC mapView] addTileSource:myTileSource]; 
     self.phase3BButtonView.backgroundColor = [UIColor blueColor]; 
    } else { 
     self.phase3BButtonView.backgroundColor = [UIColor lightGrayColor]; 
     [[FVC mapView] removeTileSource:myTileSource]; 
    } 

    NSLog(@"Map Index = %@", [[[FVC mapView] tileSources] description]); 
    if ([[[FVC mapView] tileSources] containsObject:myTileSource]) { 
     NSLog(@"YES"); 
    } else { 
     NSLog(@"NO"); 
    } 
} 

當我第一次按下按鈕時,地圖加載,我得到「是」。當我第二次按下它時,地圖不會關閉,我得到「否」。這幾乎總結我的問題

回答

1

在您的視圖控制器的接口定義,添加這個變量定義:

RMMBTilesSource *myTileSource; 

在您的視圖控制器的viewDidLoad,補充一點:

myTileSource = [[RMMBTilesSource alloc] initWithTileSetURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"MapName" ofType:@"mbtiles"]]]; 

LayerButton動作就可以變成這樣:

- (IBAction)LayerButton:(id)sender { 
    FirstViewController *FVC = [self.tabBarController.viewControllers objectAtIndex:0]; 
    self.phase3BIsChecked = !self.phase3BIsChecked; 

    if((self.phase3BIsChecked)) { 
     [[FVC mapView] addTileSource:myTileSource]; 
     self.phase3BButtonView.backgroundColor = [UIColor blueColor]; 
    } else { 
     self.phase3BButtonView.backgroundColor = [UIColor lightGrayColor]; 
     [[FVC mapView] removeTileSource:myTileSource]; 
    } 

    NSLog(@"Map Index = %@", [[[FVC mapView] tileSources] description]); 
    if ([[[FVC mapView] tileSources] containsObject:myTileSource]) { 
     NSLog(@"YES"); 
    } else { 
     NSLog(@"NO"); 
    } 
} 
+0

你被釘它!謝謝。 – user2621075