如何删除目标c中的多个标签栏项?

问题描述 投票:1回答:3
    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];

这个不能正常工作。

objective-c uitabbarcontroller
3个回答
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];

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];
© www.soinside.com 2019 - 2024. All rights reserved.