[英]UITabBarControllerDelegate's method is not called
I create programmatically a UITabBarController with more than 5 tabs (exactly 8 child view controllers). 我以编程方式创建了具有5个以上选项卡的UITabBarController(恰好是8个子视图控制器)。
My purpose is custom transition animation for tab changing. 我的目的是为切换标签而定制的过渡动画。 I setup a delegate for the UITabbarController and implement the method: 我为UITabbarController设置了一个委托并实现了该方法:
- (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
animationControllerForTransitionFromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC
When I select a tab by sending: 通过发送选择标签时:
[tabbarController setSelectedIndex: 6];
delegate's method is not called. 未调用委托的方法。
Delegate's method is called properly when I send 发送时代表的方法被正确调用
[tabbarController setSelectedIndex: 3];
for example, ie for a visible tab. 例如,对于可见标签。
One more remark: if currently selected view controller is in visible range 0..3 and next view is out of the visible range 0..3 delegate's method is still called. 再说一遍:如果当前选择的视图控制器在可见范围0..3内,而下一个视图不在可见范围0..3内,则仍然调用委托的方法。 In case when currently selected view controller is out of the visible range 0..3 and next view is out of the visible range 0..3 too delegate's method is not called. 如果当前选择的视图控制器超出可见范围0..3而下一个视图超出可见范围0..3,则也不会调用委托的方法。 And I lose my custom transition animation. 而且我丢失了自定义过渡动画。 This is my problem. 这是我的问题。
I also tried to send 我也尝试发送
[tabbarController setSelectedViewController: someViewController];
instead of 代替
[tabbarController setSelectedIndex: 6];
It doesn't work. 没用
You have too many tabs to use selectedIndex
, as your other tabs are probably in the moreNavigationController
even if the moreNavigationController
is not displayed on the screen - meaning they should return NSNotFound
when using selectedIndex
. 您有太多选项卡无法使用selectedIndex
,因为其他选项卡也可能位于moreNavigationController
即使在屏幕上未显示moreNavigationController
,这也意味着它们在使用selectedIndex
时应返回NSNotFound
。
You should use selectedViewController
instead, per iOS Docs 根据iOS文档 ,您应该改用selectedViewController
[tabbarController setSelectedViewController: yourIndexSixViewController];
I made tricky solution. 我提出了棘手的解决方案。
I collect all 8 view controllers in separated array. 我将所有8个视图控制器收集在单独的数组中。 Call it controllers . 称之为控制器 。
At start I fill tabbarController.viewcontrollers with first 5 items of controllers . 开始时,我用前五项控制器填充tabbarController.viewcontrollers 。 Further there are 2 scenarios: 此外,有两种情况:
User wants to see a tab with index out of the range 0..4 . 用户希望查看索引 范围超出0..4的选项卡。 User wants to see the tab with index 6, for example. 例如,用户希望看到带有索引6的选项卡。 And currently selected tab has index 2, for example. 例如,当前选择的选项卡具有索引2。 In this case I select a number in range 0..4 which is unequal to currently selected tab index. 在这种情况下,我选择范围为0..4的数字,该数字与当前选择的选项卡索引不相等。 For example 4. Further I replace tabbarController.viewcontrollers[4] with controllers[6] . 例如4.再进一步将tabbarController.viewcontrollers [4]替换为controllers [6] 。 After that I call 之后我打电话
[tabbarController setSelectedIndex: 4];
As result I see controllers[6] and delegate's method is called properly. 结果,我看到controllers [6]和委托的方法被正确调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.