2017-06-14 89 views
1
NSMutableArray *copyArray = [NSMutableArray arrayWithArray: 
    [self.tabBarController viewControllers]]; 
    [copyArray removeObjectAtIndex:3]; 
    [copyArray removeObjectAtIndex:4]; 
    [copyArray removeObjectAtIndex:0]; 
    [copyArray removeObjectAtIndex:1]; 
    [self.tabBarController setViewControllers:copyArray animated:false]; 

這一個工作不正常。如何刪除目標c中的多個標籤欄項目?

回答

1

爲了解決這個問題,你需要刪除降序爲了arrya指數的對象是指除去第一4然後3然後1,最後0,所以順序應該是這樣的。

NSMutableArray *copyArray = [NSMutableArray arrayWithArray: 
[self.tabBarController viewControllers]]; 
[copyArray removeObjectAtIndex:4]; 
[copyArray removeObjectAtIndex:3]; 
[copyArray removeObjectAtIndex:1]; 
[copyArray removeObjectAtIndex:0]; 
[self.tabBarController setViewControllers:copyArray animated:false]; 
+1

謝謝你,現在它的解決方案,它工作正常。 –

+0

@Gowtham歡迎伴侶:) –

0

NSMutableArray有這樣一個method

- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes; 

從文檔報價:

這種方法類似於removeObjectAtIndex:,但允許您 有效地去除與多個對象單一操作。

你可以做到這一點(本質上是相同顯示在文檔中):

NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndex:0]; 
[indexes addIndex:1]; 
[indexes addIndex:3]; 
[indexes addIndex:4]; 
[copyArray removeObjectsAtIndexes:indexes]; 
0

如果你確切地知道你要顯示其確切的項目,那麼它是什麼更容易,更容易出錯像這樣做:

NSMutableArray *newItems = [NSMutableArray arrayWithCapacity: 3]; 
newItems.append(self.tabBarController.viewControllers[x1]); 
newItems.append(self.tabBarController.viewControllers[x2]); 
newItems.append(self.tabBarController.viewControllers[x3]); 
[self.tabBarController setViewControllers:newItems animated:false];