Ios 如何在swift中检查从MDCTabBarView中选择的选项卡
我已经设置了MDCTabBarView和MDCTabBarViewDelegate,但当我点击其中一个选项卡栏项目时,该代理无法工作并打印数据。 我的代码: }Ios 如何在swift中检查从MDCTabBarView中选择的选项卡,ios,swift,Ios,Swift,我已经设置了MDCTabBarView和MDCTabBarViewDelegate,但当我点击其中一个选项卡栏项目时,该代理无法工作并打印数据。 我的代码: } 当我点击其中一个选项卡栏项目时,它确实工作并更改了选项卡,但没有调用代理。如何使代理工作,以便打印用户选择的选项卡?对于代理要为MDCTabBarView工作,您必须添加tabBarView.tabBarDelegate=self,并且必须删除shouldSelect和shouldselectioninteractionat方法,因为它
当我点击其中一个选项卡栏项目时,它确实工作并更改了选项卡,但没有调用代理。如何使代理工作,以便打印用户选择的选项卡?对于
代理
要为MDCTabBarView
工作,您必须添加tabBarView.tabBarDelegate=self
,并且必须删除shouldSelect
和shouldselectioninteractionat
方法,因为它不是tabBarDelegate
的方法,并且didSelect
委托方法将起作用。尝试过,但仍然没有调用被委托者,可以吗请更新有问题的代码?您是否删除了shouldSelect
和shouldBeginMultipleSelectionInteractionAt
方法?好的,我已经设置了tabBarView.delegate=self并更新了您代码中的代理代码,我无法看到tabBarView.tabBarDelegate=self
而不是tabBarView.delegate=self
let tabBarView = MDCTabBarView()
tabBarView.delegate = self
tabBarView.items = [
UITabBarItem(title: "Menu 1", image: nil, tag: 0),
UITabBarItem(title: "Menu 2", image: nil, tag: 1),
UITabBarItem(title: "Menu 3", image: nil, tag: 2),
UITabBarItem(title: "Menu 4", image: nil, tag: 3),
UITabBarItem(title: "Menu 5", image: nil, tag: 4),
]
tabBarView.selectedItem = tabBarView.items[0]
tabBarView.barTintColor = .brown
tabBarView.preferredLayoutStyle = .scrollableCentered
tabBarView.setTitleColor(.white, for: .normal)
tabBarView.selectionIndicatorStrokeColor = .white
view.addSubview(tabBarView)
extension ViewController: MDCTabBarViewDelegate {
func tabBarView(_ tabBarView: MDCTabBarView, didSelect item: UITabBarItem) {
print("item: \(item)")
print("item.title: \(item.title)")
if item.tag == 0 {
print("item tag 0")
}
else if item.tag == 1 {
print("item tag 1")
}
else if item.tag == 2 {
print("item tag 2")
}
else if item.tag == 3 {
print("item tag 3")
}
else if item.tag == 4 {
print("item tag 4")
}
}