如何从 View Controller 文件中以编程方式获取 View Controller 选项卡栏中的索引(最左侧选项卡从 0 开始)。
我正在用这个切换 View :
[self.tabBarController setSelectedIndex:nextIndex];
我希望能够设置:
int nextIndex = currentIndex++;
我如何获得当前指数?
编辑:以下三个答案都是正确的,谢谢大家。公平地说,我会选择最先发布的那个。
最佳答案
获取选定的索引:
NSUInteger selectedIndex = self.tabBarController.selectedIndex;
获取当前 ViewController 的索引(来自 VC):
NSUInteger selectedIndex = [self.tabBarController.viewControllers indexOfObject:self];
设置索引:
NSUInteger nextIndex = selectedIndex + 1;
if(nextIndex < [self.tabBarController.viewControllers count])
[self.tabBarController setSelectedIndex:nextIndex];
关于ios - 以编程方式获取 TabBar 中 ViewController 的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11459737/