问题描述
有人可以告诉我如何确定视图控制器在哪个tabbar索引。
以简化 - 我可以跳到一个tabBarItem的时候,通过硬编码其指数。
self.tabBarController.selectedIndex = 3;但是,如果用户自定义选项卡栏项目,则有可能是viewController在数字3处是n(n)。是用户在移动时所需要的。我如何确定它移动到哪里,以便我可以选择正确的。
任何帮助。
感谢,
李
解决方案运气与任何任何答案成功 - 但我确实排序它,所以我想解释我是怎么做,因为有其他人谁卡住试图做我做了什么。
我的标签栏控制器上的每个tabBarItem都分配了一个以0开头并以8结尾的标签。(也可以在IB中完成)
对于tabBarController等的委托是所有设置和补充以下委托方法:
- (void)tabBarController:(UITabBarController *)theTabBarController didEndCustomizingViewControllers :(NSArray *)viewControllers changed:(BOOL)changed {
NSInteger count = self.tabBarController.viewControllers.count;
NSMutableArray * tabOrderArray = [[NSMutableArray alloc] initWithCapacity:count];
(UINavigationController * viewController in viewControllers)
{
NSInteger tag = viewController.tabBarItem.tag;
[tabOrderArray addObject:[NSNumber numberWithInteger:tag]];
}
[prefs setObject:tabOrderArray forKey:@tabOrder];
[prefs synchronize]; // optional
[tabOrderArray release];
}
(注意我使用navControllers我的应用程序中的ViewControllers的ontop因此for循环)
所以现在我能做的只是做一个检查,如果有一个数组在prefs与新标签bar order
NSArray * tabBarOrder = [prefs objectForKey:@tabOrder];
if(tabBarOrder){...}
如果有一个标签栏顺序我可以得到索引的VC我想要的[[tabBarOrder indexOfObjectIdenticalTo:[NSNumber numberWithInt:theViewsTagImAfter]];
如果没有数组in prefs你可以放心地假设它没有移动,在哪里。
**
一个人自由地破坏我是如何做到这一点,如果你觉得你可以以更时尚的方式完成这一点。然而,这工作和其他建议did not。
Can somebody please tell me how I can determine which tabbar index a view controller is at.
to simplify - I can jump to a tabBarItem at the moment by hardcoding its index.
self.tabBarController.selectedIndex = 3;
However, should the user customize the tab bar items, there is a possibility the viewController at number 3 isn't the one that the user will want as it has been moved. How can I determine where it has moved to so I can select the correct one.
Any help please.
Thanks,
Lee
解决方案 ok, so I had no luck with any of any of the answers successfully - but I did sort it so thought I would explain how I did it incase there is someone else who gets stuck attempting to do what I did.
each tabBarItem on my tab bar controller was assigned a tag that started with 0 and say ended with 8. (can be done in IB also)
make sure delegate for tabBarController etc is all set and inplement the following delegate method:
- (void)tabBarController:(UITabBarController *)theTabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
NSInteger count = self.tabBarController.viewControllers.count;
NSMutableArray *tabOrderArray = [[NSMutableArray alloc] initWithCapacity:count];
for (UINavigationController *viewController in viewControllers)
{
NSInteger tag = viewController.tabBarItem.tag;
[tabOrderArray addObject:[NSNumber numberWithInteger:tag]];
}
[prefs setObject:tabOrderArray forKey:@"tabOrder"];
[prefs synchronize]; // optional
[tabOrderArray release];
}
(note i use navControllers ontop of ViewControllers in my app hence that for loop)
so now what i was able to do was simply do a check if there was an array in prefs with a new tab bar order
NSArray * tabBarOrder = [prefs objectForKey:@"tabOrder"];
if(tabBarOrder) { ... }
if there was a tab bar order i could get index of the VC i wanted with '[tabBarOrder indexOfObjectIdenticalTo:[NSNumber numberWithInt:theViewsTagImAfter]];
and if there was no array in prefs you can safely assume it hasn't moved and is where is.
**
Any one feel free to destroy how I have done this should you feel you could have accomplished this in a sleeker way. however, this works and the other suggestions didnt.
这篇关于如何确定哪个tabbar索引是我的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!