Search code examples
iosswiftuitableviewuitabbarcontroller

How to reload table view after switching tabs?


I created a UITabBarController (as TabBarMenu) like the code below; each view controller has a table view.

So I want to implement it like this: "When I do some action on AViewController, reload BViewController's table."

import UIKit

class Tbc: UITabBarController, UITabBarControllerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
    }

    func setUpBottomMenu() {
        var viewControllers = [UIViewController]()
        let img1 = UIImage(named: "img1")
        let img2 = UIImage(named: "img2")
        let AVC = AViewController()
        let BVC = BViewController()
        AVC.tabBarItem = UITabBarItem(title: "A", image: img1, tag: 0)
        BVC.tabBarItem = UITabBarItem(title: "B", image: img2, tag: 1)
        viewControllers = [AVC, BVC]

        self.viewControllers = viewControllers.map { UINavigationController(rootViewController: $0) }
    }
}

Any solutions?


Solution

  • Don't reload data on view will appear it is something that you should never do, create one protocol that will notify main tab bar controller, and then post notification if BVC was loaded to reload data, or if it wasn't never, then just pass data you want to it from main tab bar controller, in any case your your tab bar controller is their parent and that is the one that will handle how they act, not child controllers between them self.