Search code examples
iosswiftuinavigationcontrolleruinavigationbaruinavigationitem

iOS why backBarButtonItem is nil in this scenario


I have a view controller that is embed in a navigation controller, and inside it there is a button that shows another view controller, like this:

enter image description here

In the second view controller, the value of self.navigationItem.backBarButtonItem is nil, why please?

Though I can see the back button when I run the app, like this:

enter image description here

Update

I read that the back button in a view controller, is the responsibility of the navigation item of the previous view controller, and to be more presise, it is the responsibiliyt of the navigation item in the previous view controller.

so i tried to do this in the first view controller:

 self.navigationItem.backBarButtonItem!.title = "Wdd"

but i got exception because backBarButtonItem is also nil in the first view controller.

update2

Guys look here please

https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html#//apple_ref/doc/uid/TP40011313-CH2-SW3

there is this sentence

The navigation item associated with the current view controller provides the content for the center and right positions of the navigation bar. The navigation item for the previous view controller provides the content for the left position


Solution

  • I found the solution myself.

    In the firstVC i do this in view did load:

    backButton.title = "ddddd"
            self.navigationItem.backBarButtonItem = backButton
    

    and it works

    i got the answer after this image :

    enter image description here