我有一个基于标签栏的应用程序.所有选项卡都有一个导航控制器作为根.如果标签处于活动状态,用户再次点击标签,它会弹回导航控制器.
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.
我怎样才能阻止这种行为?
How can I stop this behavior?
所以实际上我有一个导航控制器 + 一个隐藏的 viewcontroller
可以做出一些决定 + 另一个视图控制器.对于原始问题中的误导性信息,我们深表歉意.我对所有选项卡(其中 3 个)使用隐藏的 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;
}
}
或者用一种不那么冗长的方式:
or in a less verbose way:
- (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));
}
这篇关于如何停止弹出到导航控制器的标签栏的第二次点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!