如何停止跳到导航控制器的选项卡栏的第二个点击? [英] How to stop tab bar's second tap which pops to navigation controller?
问题描述
我有一个基于标签栏的应用程序.所有选项卡均以导航控制器为根.如果用户再次点击该选项卡(如果该选项卡处于活动状态),它将弹出回到导航控制器.
I have a tab bar based app. All tabs have a navigation controller as the root. If the user taps on the tab again if the tab is active, it pops back to the navigation controller.
如何停止这种行为?
因此,实际上我有一个导航控制器+一个隐藏的 viewcontroller
,它可以做出一些决定+另一个视图控制器.很抱歉原问题中的误导性信息.我对所有选项卡使用了隐藏的 viewcontroller
,其中三个选项卡,因为如果用户未登录,则所有三个选项卡都具有登录屏幕.如果用户登录,则弹出登录屏幕,然后在每个标签上分别放置 1,2,3
个单独的 viewcontrollers
.
So in fact I have a navigation controller + a hidden viewcontroller
that makes some decisions + another view controller. Sorry for the misleading information in the original question. I use the hidden viewcontroller
for all the tabs, 3 of them, since I have the login screen on all 3 if the user is not logged in. If the user logs in, then I pop the login screen, and put the 1,2,3
individual viewcontrollers
on each tab.
第一次点击:
0 : class=Crossing: 0x645c8a0>
1 : class=FavoritesViewController: 0x64ac140>
shouldSelectViewController : UINavigationController
UINavigationController topclass:FavoritesViewController
myTabBarController.selectedViewController :UINavigationController
did disappear
didSelectViewController : UINavigationController
UINavigationController topclass:FavoritesViewController
第二次点击:
0 : class=Crossing: 0x645c8a0>
1 : class=FavoritesViewController: 0x64ac140>
shouldSelectViewController : UINavigationController
UINavigationController topclass:FavoritesViewController
myTabBarController.selectedViewController :UINavigationController
didSelectViewController : UINavigationController
UINavigationController topclass:Crossing
推荐答案
@MarkGranoff在执行此操作的正确轨道上,但是执行此操作的方法是执行以下操作:
@MarkGranoff was on the right track for doing this, but the way to do it is by doing something like this:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
if ([tabBarController.viewControllers indexOfObject:viewController] == tabBarController.selectedIndex)
{
return NO;
}
else
{
return YES;
}
}
或以不太冗长的方式:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
return (viewController != tabBarController.selectedViewController);
}
如果您只想阻止某个选项卡的默认行为,则可以执行以下操作:
If you only want to block the default behaviour for a certain tab then you can do something like this:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
NSUInteger indexOfNewViewController = [tabBarController.viewControllers indexOfObject:viewController];
// Only the second tab shouldn't pop home
return ((indexOfNewViewController != 1) ||
(indexOfNewViewController != tabBarController.selectedIndex));
}
这篇关于如何停止跳到导航控制器的选项卡栏的第二个点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!