2017-06-14 92 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];