【问题标题】:Select tab programmatically custom tab bar ios以编程方式选择选项卡自定义选项卡栏ios
【发布时间】:2017-07-07 05:39:30
【问题描述】:

我已经使用下面的tutorial在ios中实现自定义标签 当我点击任何项目时,它工作正常。现在我想以编程方式移动它。例如我收到了 Firebase 的通知表,想打开第三个标签。但我越来越零了。我在appdelegate 中引用了MainTabbarController 实例。 这是我尝试过的 在CustomTabBar

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

AppDelegate

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

【问题讨论】:

    标签: ios swift3 custom-controls


    【解决方案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) 与你要切换的标签的索引。

    【讨论】:

    • 您是否尝试从 AppDelegate 调用它。我越来越零了。调试器说 CustomTabBar 的 selectedTabBarItemIden 是 nill
    • 这意味着没有任何先前选择的项目。因此,在这种情况下,您可能不必正确显示动画。因此,如果 selectedTabBarItemIndex 为 nil,您可以跳过该语句 animateTabBarSelection。
    • 将其放入 nil 检查无效。虽然问题已经解决了。它与 customtabbar 实例有关。我在初始化 customtabbar 之前引用了 App Delegate
    猜你喜欢
    • 1970-01-01
    • 2016-12-30
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    相关资源
    最近更新 更多