-1

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.

Pmontor
  • 3
  • 2

1 Answers1

0

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.
}

}

piousLogic
  • 16
  • 2
  • Hey, thanks for your help. I tried that already and it doesn't work. I do believe it's a problem with the navigation stack. I even check if the navbar is hidden and it says false, so that's not the issue :S – Pmontor Jun 30 '17 at 14:31
  • Hi I edited answer, tested on iOS 10.0 and with detail. Basically, needs to be visible to work. If still give you an issue try saving a strong reference to it. – piousLogic Jul 02 '17 at 17:30
  • Hi, as I said in my previous comment, I had already tried that: navigationController?.setNavigationBarHidden(false, animated: true) and still don't see the nav bar. I'm even printing navigationController?.isNavigationBarHidden and it returns false all the time (without me doing anything programmatically), so it's definitely not that. – Pmontor Jul 03 '17 at 20:32
  • It actually works. I was doing it in viewDidLoad instead of doing it in viewDidAppear(). Any idea why this is happening? The issue now is that the nav bar appears in the screen after the rest of the view and looks kind of odd. Any ideas? – Pmontor Jul 05 '17 at 18:55
  • Hi, just put it in the viewWillAppear() and change animated to false. It's pretty good like that, or if you need a longer setup time - just add a loading animation. – piousLogic Jul 08 '17 at 12:41