How can you update a label when the tab/view controller of a tab bar changes? (swift)

Viewed 488

I am working on a app with a tab bar with four separate tabs. I for example save a number that can be changed/modified in three of those tabs. A label, that displays that number is included in all four of those tabs. However, when I change the number in one tab, the labels of the other tabs are not updated when I switch tabs.

I tried including this in each viewDidLoad() of the view controllers of the tabs:

self.tabBarController?.delegate = self 

and then used:

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        let tabBarIndex = tabBarController.selectedIndex
        if tabBarIndex == 2{
            updateLabel()
        }
}

If I do that in all viewControllers and select the tabs, the view is still changed, but the tabBarController fails, so that the function updateLabel() isn't called at all. If I only include the code in the First View Controller and expand this part:

if tabBarIndex == 2{
            updateLabel()
        }

to cover all tabs, the respective functions (updateLabel()) of the classes are called but the Label itself is nil.

@IBOutlet weak var HoursLabel: UILabel!

func updateLabel(){
        if HoursLabel != nil{
             //code
        }    
}

And the label isn't updated. Does someone know how to fix this? Thank you so much in advance :)

1 Answers

hey i got some solutions for you hope this is what you want.

i'm updating your tab label value with Notification Center here is some code

// TabbarController Code

import UIKit

class TabbarViewController: UITabBarController, UITabBarControllerDelegate {

//MARK Life View Cycle
override func viewDidLoad() {
    super.viewDidLoad()

    tabBarController?.delegate = self
    NotificationCenter.default.addObserver(self, selector: #selector(self.TabbarNoitifuntionCall), name: NSNotification.Name(rawValue: "CallTabBarNotificationsCenter"), object: nil) // this code will call when ever you update your value in view controller
}

//MARK:- Private Functions
@objc func TabbarNoitifuntionCall(_ notification: Notification) {
    self.viewControllers![0].title = "First " + String(notification.object as! Int)
    self.viewControllers![1].title = "Second " + String(notification.object as! Int)
   }

}

// First Tabbar Controller

 import UIKit

 class FirstVc: UIViewController {

//MARK:- IBOutlet
@IBOutlet weak var update_lbl: UILabel!

//MARK:- Variables
var count = Int()

//MARK:- Life View Cycle
override func viewDidLoad() {
    super.viewDidLoad()
}

//MARK:- Private Function
@IBAction func buttonAction(_ sender: UIButton) {
    
    count = count + 1
    update_lbl.text = String(count)
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "CallTabBarNotificationsCenter"), object: count) // here we will call notifications center so our labal value upadate
    
   }

}

// Second Tabbar Controller

  import UIKit

   class SecondVc: UIViewController {

//MARK:- IBOutlet
@IBOutlet weak var update_lbll: UILabel!

//MARK:- Variables
var count = Int()

//MARK:- Life View Cycle
override func viewDidLoad() {
    super.viewDidLoad()
}

//MARK:- Private Function
@IBAction func button_action(_ sender: Any) {
    count = count + 1
    update_lbll.text = String(count)
    
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "CallTabBarNotificationsCenter"), object: count) // here we will call notifications center so our labal value upadate
   }

}

i hope this is the simple way if anyone add something then please go head Thank you.

Related