Search code examples
iosswiftuinavigationcontrolleruinavigationbarsegue

How do I make navigation bar to show in root viewcontroller A the second time it's presented? (A>B>A)


I'm developing a shopping list like app where I have a Navigation Controller and the root view controller is the screen where the user can search for products (SearchViewController). When the user selects a product it segues to DetailViewController. This view has an option to check out or add more products. If users click on "Add more products" I have to segue to SearchViewController so they can search for more products. I want to present this VC again but I want the Nav Bar to show this time since I want to be able to go back if I decide not to add any other products. Right now I'm sending the shoppingContext in the segue to determine from the SearchVC if I come from "DetailsVC" or not. I think there's a problem with the way I'm adding view controllers to the navigation stack, but I've never encountered a problem like this and don't know what else to try. With my current implementation (performSegue from DetailsVC to SearchVC) any time I click on a new item it segues twice to the Details screen, which I suspect may also be caused by the same navigation stack issue.

I tried creating a new object of SearchVC and pushing it to the stack instead of performing the segue but it didn't work either.

What can I do to fix it?

Basically, in detailsVC I do the following:

let segueAction = SegueAction(name: "segueToSearch", preparer: { 
destinationVC in
        if
            let activeVC = destinationVC as? SearchViewController
        {
            activeVC.shoppingList = self.shoppingViewModel.shoppingList
        }
    })

 performSegue(withIdentifier: segueAction.name, sender: segueAction)

The segue "segueToSearch" is a Show (push) type segue.

Then in the SearchVC I check if shoppingList != nil and if so do:

navigationController?.setNavigationBarHidden(false, animated: false)

If I check if the navigation bar is hidden it returns false but I still don't see it.


Solution

  • Hi it's pretty straight forward. Answer can be found here: Navigation bar show/hide

    [[self navigationController] setNavigationBarHidden:NO animated:YES];

    And I would put a property to check in the viewWillApear.

    -- EDIT: --

    TESTED: I added it to a button action, works also in the viewDidAppear when dismiss back from to detail.

    Hope it helps.

    class ViewController: UIViewController {

    var didHideNav: Bool = false
    
    
    
    @IBAction func changeHidden(_ sender: UIButton) {
    
        if !didHideNav {
            print("Should Be Hidden")
            self.navigationController?.setNavigationBarHidden(true, animated: true)
            didHideNav = true
        }else{
            print("Should Be Visible")
            self.navigationController?.setNavigationBarHidden(false, animated: true)
            didHideNav = false
        }
    
    
    }
    
    override func viewDidAppear(_ animated: Bool) {
        if !didHideNav {
            print("Should Be Hidden")
            self.navigationController?.setNavigationBarHidden(true, animated: true)
            didHideNav = true
        }else{
            print("Should Be Visible")
            self.navigationController?.setNavigationBarHidden(false, animated: true)
            didHideNav = false
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    }