2017-07-07 153 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

我已經更新了答案,請檢查 –