阻止tabbar更改特定索引处的选项卡 - IOS [英] Prevent tabbar from changing tab at specific index - IOS
问题描述
感谢您阅读我的问题。
当用户点击索引为4的标签时,我正在尝试实现弹出菜单。所以我正在尝试防止tabbar在按下索引4时切换viewcontroller。
I'm trying to implement a popup menu when a user clicks the tab with the index of 4. So I'm trying to prevent the tabbar from switching viewcontroller when index 4 is pressed.
这是我的代码:
- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
if(viewController == [tabBarController.viewControllers objectAtIndex:4]){
NSLog(@"NO");
return NO;
}else{
NSLog(@"YES");
return YES;
}
}
我已实施 UITabBarControllerDelegate
和 self.delegate = self;
在 viewDidLoad
中只工作一次。
I've implemented the UITabBarControllerDelegate
and self.delegate = self;
in the viewDidLoad
and it works but just one time.
单击索引4选项卡时,菜单显示且标签栏未切换视图( GREAT ),但是当我再次单击它时即使我得到LogNO,视图也会改变。
这可能是什么问题?
When I click the index 4 tab the menu shows and the tabbar doesn't switch view (GREAT), but when I click it again the view changes even if I get the Log "NO". What could be the problem here?
感谢您的任何建议!
已解决
感谢Kasaname在下面的回答,我通过添加selectedindex并将其设置为标志索引(prevtab)来解决它。我将prevtab更改为上次选择的选项卡的索引,当用户选择索引4时将其更改。
我的最终代码:
Thanks to Kasaname's answer below I solved it by adding selectedindex and set it to a flag index (prevtab). I change the prevtab to the index of the last selected tab, exept for when the user selects index 4. My final code:
- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
if(viewController == [tabBarController.viewControllers objectAtIndex:4]){
self.selectedIndex = prevTab; //only change in this method
return NO;
}else{
return YES;
}
}
推荐答案
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == 0) {
} else if (tabBarController.selectedIndex == 1) {
} else if (tabBarController.selectedIndex == 2) {
}
}
为什么不使用此委托
使用此委托它将起作用我猜想
why dont u use this delegate Use this delegate it will work i suppose
这篇关于阻止tabbar更改特定索引处的选项卡 - IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!