如何删除目标c中的多个标签栏项目?
问题描述:
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中的多个标签栏项目?
答
为了解决这个问题,你需要删除降序为了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];
答
NSMutableArray
有这样一个method:
- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes;
从文档报价:
这种方法类似于
removeObjectAtIndex:
,但允许您 有效地去除与多个对象单一操作。
你可以做到这一点(本质上是相同显示在文档中):
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndex:0];
[indexes addIndex:1];
[indexes addIndex:3];
[indexes addIndex:4];
[copyArray removeObjectsAtIndexes:indexes];
答
如果你确切地知道你要显示其确切的项目,那么它是什么更容易,更容易出错像这样做:
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];
谢谢你,现在它的解决方案,它工作正常。 –
@Gowtham欢迎伴侣:) –