Search code examples
swiftuinavigationcontroller

Swift - How to access Navigation Bar from another controller


So, my problem is, I have a collection view inside a table view, and I want to add a bar button item to the navigation bar inside the table view controller from the collection view controller. Can I actually do that?

*I've tried to pass the navigation controller from the table view controller to the collection view controller and then add the button from the collection view controller, but nothing happens.


Solution

  • You have a UINavigationController whatever you push into this navigation you could access the navigation without passing it

    self.navigationController

    the NavigationBar is shared between all the controllers will be pushed into the navigation stack

    the navigation item is only related to your controller

    so if you added a navigation bar for A controller is a different navigation bar for the B controller even if both of them pushed to the same navigation controller

            self.navigationItem.leftBarButtonItem = .init(title: "Ate", style: .done)