我有一个UITabBar
,有4个标签。
我希望当按下ViewController
两次时,将在标签的每个tabBarItem
中调用一个特定的函数(例如,用户现在在ProfileVC
上并按下{ {1}}项目,我想刷新视图。)
如何检测用户何时按下选项卡上的视图?
谢谢!
答案 0 :(得分:1)
一个选项是在viewWillAppear()
方法内部进行刷新。第二个是相当长的。
在父视图控制器中
protocol ParentDelegate {
func refresh()
}
class LandingViewController: UIViewController, UITabBarDelegate {
var delegate: ParentDelegate?
var selectedItem: UITabBarItem!
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.delegate = self
self.selectedItem = self.tabBar.selectedItem
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SegueNameForDestinationViewController1" {
if let vc = segue.destinationViewController as? YourDestinationViewController1 {
self.delegate = vc.self
}
} else if segue.identifier == "SegueNameForDestinationViewController2" {
if let vc = segue.destinationViewController as? YourDestinationViewController2 {
self.delegate = vc.self
}
}
}
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
if self.selectedItem == item {
self.delegate?.refresh()
}
self.selectedItem = item
}
}
在每个标签视图控制器中,
class TabViewController: UIViewController, ParentDelegate {
func refresh() {
//write your code here
}
}