2017-07-07 174 views
2

我已经使用以下tutorial在ios中实现自定义选项卡 当我点击任何项目时,它的工作正常。现在我想以编程方式移动它。例如我收到Firebase通知表单,并想要打开第三个标签。但我得到零。我参考了appdelegate中的MainTabbarController实例。 以下是我已经尝试 在CustomTabBar以编程方式选择标签自定义标签栏ios

func customTapped(index :Int){ 
     animateTabBarSelection(from: selectedTabBarItemIndex, to: index) 
     selectedTabBarItemIndex = index 
     delegate.didSelectViewController(self, atIndex: index) 
} 

AppDelegate

mainTabBarController?.customTabBar?.customTapped(index: 2) 

回答

1

从您已经与问题一起提供的链接,下面是我发现是负责的声明切换标签

func barItemTapped(sender : UIButton) { 
    let index = tabBarButtons.indexOf(sender)! 

    animateTabBarSelection(from: selectedTabBarItemIndex, to: index) 
    selectedTabBarItemIndex = index 
    delegate.didSelectViewController(self, atIndex: index) 
} 

您可以修改此代码为

func barItemTapped(sender : UIButton) { 
    let index = tabBarButtons.indexOf(sender)! 
    tabSelectedAt(index) 

} 

func tabSelectedAt(_ index:Int) { 
if selectedTabBarItemIndex != nil { 
    animateTabBarSelection(from: selectedTabBarItemIndex, to: index) 
    } 
    selectedTabBarItemIndex = index 
    delegate.didSelectViewController(self, atIndex: index) 
} 

所以调用功能func tabSelectedAt(_ index:Int)与您要切换的选项卡的索引。

+0

你是否试图从AppDelegate中调用它。我得到零。调试器说CustomTabBar的selectedTabBarItemIden是nill –

+0

这意味着没有任何先前选择的项目。所以在这种情况下,您可能不必正确显示动画。因此,如果selectedTabBarItemIndex为零,则可以跳过该语句animateTabBarSelection。 –

+0

我已经更新了答案,请检查 –

相关问题