0
votes

I have a UITabBar with it's own UITabBarController and in this controller I'm doing some adjustments to the tabBar such as disabling/enabling tabBar items. I'm doing it in viewDidLoad.

Trigger for these adjustments however will be on one of the related tabs and it's view controller. The trigger will be a button and once the button is pressed one of the items on the tabBar should go from "disabled" to "enabled".

Is there any tabBar reload function that I need to use or do I need to somehow reload UITabBarController to make this happen?

Thanks - I appreciate any help you can provide!

Edit: My VCs for UITabBarController and UIViewController are below.

UITabBarController

class TabBarVC: UITabBarController, TopRatedVCDelegate {


var tabBarItem1 = UITabBarItem()
var tabBarItem2 = UITabBarItem()
var tabBarItem3 = UITabBarItem()
var tabBarItem4 = UITabBarItem()

override func viewDidLoad() {
    super.viewDidLoad()
}

func setbar() {

    let tabBarControllerItems = tabBar.items

    if let tabArray = tabBarControllerItems {
        tabBarItem1 = tabArray[0]
        tabBarItem2 = tabArray[1]
        tabBarItem3 = tabArray[2]
        tabBarItem4 = tabArray[3]
    }
        tabBarItem1.isEnabled = true
        tabBarItem1.badgeValue = "99"
        tabBarItem2.isEnabled = false
        tabBarItem3.isEnabled = true
        tabBarItem4.isEnabled = true
}
}

UIViewController with button

protocol TopRatedVCDelegate: class {
    func setbar()
}

class TopRatedVC: UIViewController {

@IBOutlet weak var userDefValueLbl: UILabel!

weak var delegate: TopRatedVCDelegate?

override func viewDidLoad() {
    super.viewDidLoad()

}


@IBAction func btnPressed(_ sender: Any) {
    delegate?.setbar()
}
}
1
You likely want to use Protocol / Delegate pattern. That will allow your trigger button to "call back" to your custom UITabBarController where you can handle disabling / enabling tabs (or whatever else you want to do).DonMag
This sound like it can work. Do you have any example how I can do that? I actually tried it in a different way by saving some value into UserDefaults once the button is pressed and in performing the check but in UITabBarController but this worked only once the tabBar was loaded. I would like it to work anytime I want - so to call the "reload" of the bar once the button is clicked for the checked to be performed...Petr G

1 Answers

0
votes

Let me explain to you the delegation pattern. It's an efficient way to communicate between components like controllers and views. You need to follow 6 steps :

  1. You need to add a protocol with your requirements. For your case, you can add a function called updateTabItems to enable or disable tab bar items.
  2. You need to add a delegate property with the protocol type that you just create like this : weak var delegate: ProtocolType?
  3. Call the function of your delegate property like this : delegate?.updateTabItems()
  4. On your second controller, view or component, it needs to conform to your protocol like this class Controller : YourProtocol
  5. Then, you have to set the delegate property to self
  6. Finally implement the method updateTabItems

Here is the full explanation of the delegation pattern. Very important and very useful on your daily routine as IOS developer : Delegation pattern