在页面

时间:2016-08-08 17:32:14

标签: ios objective-c swift cocoa-touch uitabbarcontroller

我有一个UITabBar,有4个标签。

我希望当按下ViewController两次时,将在标签的每个tabBarItem中调用一个特定的函数(例如,用户现在在ProfileVC上并按下{ {1}}项目,我想刷新视图。)

如何检测用户何时按下选项卡上的视图?

谢谢!

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
    }
}