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];
这个不能正常工作。
要解决这个问题,你需要按照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];