Programmatically get index of a ViewController in the TabBar
質問
How do you programmatically get the index in the tab bar (starts at 0 for leftmost tab) of a view controller, from within the View Controller File.
I am switching views with this:
[self.tabBarController setSelectedIndex:nextIndex];
and I want to be able to just set:
int nextIndex = currentIndex++;
How do I get that Current Index?
EDIT: All three of the below answers are correct, thanks guys. To be fair I will choose the one that was posted first.
解決
Get selected index:
NSUInteger selectedIndex = self.tabBarController.selectedIndex;
Get index of current ViewController (from within the VC):
NSUInteger selectedIndex = [self.tabBarController.viewControllers indexOfObject:self];
Set index:
NSUInteger nextIndex = selectedIndex + 1;
if(nextIndex < [self.tabBarController.viewControllers count])
[self.tabBarController setSelectedIndex:nextIndex];
他のヒント
You can get the current index of tabController with the property selectedIndex
:
[self.tabBarController selectedIndex]
Also if you want get the index of a ViewController you can access to the array of the tabs with the property viewControllers
.
Chech the Apple doc for more info.
所属していません StackOverflow