ios - 以编程方式获取 TabBar 中 ViewController 的索引

标签 ios tabview

如何从 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/

相关文章:

ios - 如何为后退按钮做 'prepareSegue'?

ios - Swift 下标错误

ios - 过滤具有大量对象的数组的唯一名称

ios - 将条形按钮项目从播放更改为暂停 Swift

ios - Metal与GLSL CoreImage性能

java - 如何在 Primefaces tabView 中设置 Activity 选项卡?

validation - primefaces tabView 在选项卡更改时跳过表单验证

css - 想知道如何通过 tabview 样式属性更改标题?

JSF/PrimeFaces : Navigate from one tab to another

ios - SwiftUI 通过 TabView 以模态方式呈现 View ?