如何停止弹出到导航控制器的标签栏的第二次点击?
我有一个基于标签栏的应用程序。所有选项卡都有一个导航控制器作为根。 如果用户再次点击该选项卡(如果该选项卡处于活动状态),它将弹回到导航控制器。
我怎样才能阻止这种行为?
所以实际上我有一个导航控制器 + 一个隐藏的 viewcontroller
来做出一些决定 + 另一个视图控制器。对于原始问题中的误导性信息,我们深表歉意。我对所有选项卡(其中 3 个)使用隐藏的 viewcontroller
,因为如果用户未登录,我在所有 3 个选项卡上都有登录屏幕。如果用户登录,那么我会弹出登录屏幕,并将 1,2,3
单独的 viewcontrollers
放在每个选项卡上。
第一次点击:
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
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?
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.
First tap:
0 : class=Crossing: 0x645c8a0>
1 : class=FavoritesViewController: 0x64ac140>
shouldSelectViewController : UINavigationController
UINavigationController topclass:FavoritesViewController
myTabBarController.selectedViewController :UINavigationController
did disappear
didSelectViewController : UINavigationController
UINavigationController topclass:FavoritesViewController
Second tap:
0 : class=Crossing: 0x645c8a0>
1 : class=FavoritesViewController: 0x64ac140>
shouldSelectViewController : UINavigationController
UINavigationController topclass:FavoritesViewController
myTabBarController.selectedViewController :UINavigationController
didSelectViewController : UINavigationController
UINavigationController topclass:Crossing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
@MarkGranoff 这样做是正确的,但方法是通过执行以下操作:
或者以更简洁的方式:
如果您只想阻止某个选项卡的默认行为,那么您可以执行类似的操作这:
@MarkGranoff was on the right track for doing this, but the way to do it is by doing something like this:
or in a less verbose way:
If you only want to block the default behaviour for a certain tab then you can do something like this:
Swift 3
子类化您的
UITabBarController
并在该类上实现UITabBarControllerDelegate
。class viewMain: UITabBarController, UITabBarControllerDelegate {
在
viewDidLoad
中,将类的委托设置为其自身self.delegate = self
添加此功能
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) ->布尔{
return (viewController != tabBarController.selectedViewController);
}
Swift 3
Subclass your
UITabBarController
and implementUITabBarControllerDelegate
on that class.class viewMain: UITabBarController, UITabBarControllerDelegate {
In
viewDidLoad
, set your class's delegate to be itselfself.delegate = self
Add this function
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
return (viewController != tabBarController.selectedViewController);
}
在您的应用程序委托(我假设是 UITabBarController 的委托)中,尝试实现
tabBarController:shouldSelectViewController:
,如果要选择的视图控制器不在其根视图,则返回 NO。当然,这可能会适得其反,因为当您尝试返回时,这些选项卡已被切换离开......嗯......In your application delegate (which I assume is the delegate for your UITabBarController), try implementing
tabBarController:shouldSelectViewController:
and return NO if the view controller to be selected is not at its root view. This, of course, may backfire for tabs that were switched away from when you try to go back to them... Hmm....更新 Swift 4.1
谢谢!!!
Update Swift 4.1
Thanks!!!
您可以检查视图控制器
并跟踪所选的视图控制器。
You can check for the view controller in
and you can keep track on the view controllers selected.