如何单击 UITabBarItem 并显示新的 ViewController?
Posted
技术标签:
【中文标题】如何单击 UITabBarItem 并显示新的 ViewController?【英文标题】:How to click UITabBarItem and show new ViewController? 【发布时间】:2018-08-07 03:48:50 【问题描述】:我手动添加标签栏来查看控制器,这不是tabBarController
,因为我只想要标签栏UI,而不是标签栏功能
我想点击标签栏并显示新的ViewController
,但我不能在storyBoard
中这样做
如果我使用这个方法,xcode会提示我Value of type 'UITabBarItem' has no member 'isUserInteractionEnabled'
和Value of type 'UITabBarItem' has no member 'addGestureRecognizer'
如何单击 UITabBarItem?
How to make a UILabel clickable?
@IBOutlet weak var setting: UITabBarItem!
@IBOutlet weak var activity: UITabBarItem!
@IBOutlet weak var profile: UITabBarItem!
@IBOutlet weak var connect: UITabBarItem!
@IBOutlet weak var scanner: UITabBarItem!
override func viewDidLoad()
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction))
setting.isUserInteractionEnabled = true
setting.addGestureRecognizer(tap)
activity.isUserInteractionEnabled = true
activity.addGestureRecognizer(tap)
profile.isUserInteractionEnabled = true
profile.addGestureRecognizer(tap)
connect.isUserInteractionEnabled = true
connect.addGestureRecognizer(tap)
scanner.isUserInteractionEnabled = true
scanner.addGestureRecognizer(tap)
@objc func tapFunction(sender:UITapGestureRecognizer)
print("tap working")
【问题讨论】:
【参考方案1】:“UITabBarItem”类型的值没有成员“isUserInteractionEnabled”,“UITabBarItem”类型的值没有成员“addGestureRecognizer”
因为UITabBarItem继承自UIBarItem,即继承自NSObject,而不是UIView
要启用/禁用
UITabBarItem
,您可以使用isEnabled
属性
https://developer.apple.com/documentation/uikit/uibaritem/1616418-isenabled?changes=_2
要捕捉
UITabBarItem
的动作,你可以在UITabBar的代理tabbar:didselectitem
中捕捉它
https://developer.apple.com/documentation/uikit/uitabbardelegate/1623463-tabbar?changes=_2
并通过UITabBarItem.tag
获得项目的位置
https://developer.apple.com/documentation/uikit/uibaritem/1616419-tag?changes=_2
【讨论】:
我在didSelect
打印项目标签,所有标签都是0
@CLSo : 你为 UITabbarItem 设置了标签吗?在故事板或代码中 UITabbarItem.tag = tag_index
我明白了,现在可以了以上是关于如何单击 UITabBarItem 并显示新的 ViewController?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用自定义 UIImage 作为 UITabBarItem 徽章?