iosswiftswift5tabbarcontroller

How to use advanced navigation on Tab Bar Controller


I have a TabBarController with 5 options. First TabBarItem is HomeScreen, second is taking me to a deep navigation workflow. When I'm on the last ViewController of that deep navigation I have a Save button and when I click it I want to send the user back on the HomeScreen. The problem is that all my icons from TabBarController disappear. I've tried another solution but for that solution my icons are showing but when I click the second TabBarItem is showing me the last screen of that deep navigation workflow. Here is my project: https://github.com/AdvancedNavigationTabBarController Here is my code:

FIRST TAB BAR ITEM

class HomeVC: UIViewController {
    
    @IBOutlet var textfieldHoldingScootersValue: UITextField!
    @IBOutlet var textfieldHoldingBicyclesValue: UITextField!
    
    var valueForScootersScreen = String()
    var valueForBicyclesScreen = String()
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        print("Selected index: \(self.tabBarController?.selectedIndex ?? -1)")
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        
        valueForScootersScreen = textfieldHoldingScootersValue.text ?? String()
        valueForBicyclesScreen = textfieldHoldingBicyclesValue.text ?? String()
        
        if let navController = self.tabBarController?.viewControllers?[1] as? UINavigationController{
            if let scootersTab = navController.children.first as? BuyNewScooterOrBikeVC{
                scootersTab.receivedValueFromHomeScreen = valueForScootersScreen
            }
        }
        
        if let navController = self.tabBarController?.viewControllers?[2] as? UINavigationController{
            if let bicyclesTab = navController.children.first as? BuyNewScooterOrBikeVC{
                bicyclesTab.receivedValueFromHomeScreen = valueForBicyclesScreen
            }
        }
}

SECOND TAB BAR ITEM

class BuyNewScooterOrBikeVC: UIViewController {
    
    @IBOutlet var receivedValueLabel: UILabel!
    
    var receivedValueFromHomeScreen = String()
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        print("Selected index: \(self.tabBarController?.selectedIndex ?? -1)")
        
        if self.tabBarController?.selectedIndex == 1 {
            receivedValueLabel.text = "Value received for Scooters: \n" + receivedValueFromHomeScreen
            self.navigationItem.title = "Buy a Scooter"
        }

        if self.tabBarController?.selectedIndex == 2 {
            receivedValueLabel.text = "Value received for Bicycles: \n" + receivedValueFromHomeScreen
            self.navigationItem.title = "Buy a Bicycle"
        }
    }
}

LAST VIEW CONTROLLER OF SECOND TAB BAR ITEM


class ScooterOrBikeLastVC: UIViewController {

    @IBAction func saveBtnTapped(_ sender: UIButton) {
        
        // Navigate to Home Screen
        // self.parent?.navigationController?.popToRootViewController(animated: true) // if I use this line then when I click on the same Tab Bar is sending me to the last screen and I want to be on the first screen of that navigation workflow
        self.navigationController?.popToRootViewController(animated: true) // If I use this line when I click Save btn I don't see anymore the icons on Tab Bar Controller
        self.tabBarController?.selectedIndex = 0
    }
}

Thanks for reading this !


Solution

  • set tabIndex to 0 before popping to root.

    self.tabBarController?.selectedIndex = 0
    self.navigationController?.popToRootViewController(animated: true)
    

    // class ScooterOrBikeLastVC // self.parent?.navigationController?.popToRootViewController(animated: true) // This line does nothing as the parent is nil Why do you get the Home tab then? because you have set tab bar to 0 self.tabBarController?.selectedIndex = 0

    You should think of better ways to communicate between the controllers when you wish to write the Production code. In your current approach, any controller can access and modify the other.