ios 如何以编程方式设置 UITabBarController 的选定选项卡,同时在 UITabBarControllerDelegate 中触发 shouldSelectViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17676718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I programmatically set selected tab of UITabBarController while also triggering shouldSelectViewController in UITabBarControllerDelegate
提问by herrtim
I'm trying to animate the transitions between tabs in my UITabBarController, which is working fine when I push on the tab buttons. However, when I switch tabs programmatically by calling
我正在尝试为我的 UITabBarController 中的选项卡之间的转换设置动画,当我按下选项卡按钮时,它工作正常。但是,当我通过调用以编程方式切换选项卡时
[self.tabBarController setSelectedIndex:2];
in a swipe gesture recognizer, the shouldSelectViewController function is NOT being called in my UITabBarControllerDelegate delegate, and therefore my animation isn't being triggered.
在滑动手势识别器中,我的 UITabBarControllerDelegate 委托中没有调用 shouldSelectViewController 函数,因此我的动画没有被触发。
Is there a way to accomplish what I want? Can I programmatically trigger the tab switch differently perhaps so that the shouldSelectViewController function gets called?
有没有办法实现我想要的?我可以以不同的方式以编程方式触发选项卡切换,以便调用 shouldSelectViewController 函数吗?
回答by Kapil Choubisa
If you have implemented - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
in your tabBarController's delegate than you can call it manually.
如果您已- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
在 tabBarController 的委托中实现,则可以手动调用它。
[self.tabBarController.delegate tabBarController:self.tabBarController shouldSelectViewController:[[tabBar viewControllers] objectAtIndex:2]];
[self.tabBarController setSelectedIndex:2];
Hope this helps.
希望这可以帮助。