0

I am reading about UINavigationController, and it makes sense to have a navigationItem in the NavigationController,

but when I create a UIViewController, there is also a property called navigationItem from type UINavigationItem

so now I have :

  1. navigationItem in the UIViewController

  2. navigationItem in the UINavigationController

do they have different purposes ?

It sounds like the navigationItem in the UINavigationController is not working because I did this code in a UIViewController:

self.navigationController!.navigationItem.title = "test2"

and the title didn't change, while I did this code:

 self.navigationItem.title = "test"

and the title changed

sarah
  • 1,201
  • 1
  • 9
  • 29
  • what is your query in your questions? – sandeep tomar May 27 '16 at 09:44
  • if u don't have navigation item or bar in Uiviewcontroller where you would add buttons and other things while your are navigating from one screen to another screen .like back button and other button , it's seem look good when you are using navigation bar in your applicastion – Darshan May 27 '16 at 09:44
  • 1
    See http://stackoverflow.com/questions/16913332/navigationcontroller-navigationitem-vs-navigationitem. – Martin R May 27 '16 at 09:45
  • @sandeeptomar my question is do they have different purposes ? – sarah May 27 '16 at 09:47
  • @Gamex wait ... *if u don't have navigation item or bar in Uiviewcontroller* well indeed there is no navigation bar in UIviewcontroller. Plus, changing the back button (or the toolbar) is the responsible of the ui navigation controller not uiviewcontroller, which is responsible just for its data, [read here please](https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html) – sarah May 27 '16 at 09:49
  • @MartinR thank you, that links helped me understanding it. So if I want the `self.navigationController!.navigationItem.title = "test2"` I would need to embed the naviagtion controller into another naivagiont contoroller – sarah May 27 '16 at 09:55
  • @Sara what is the purpose for last comment ? self.title just sets title for current ViewController navigation item – Muhammad Adnan May 27 '16 at 09:59

2 Answers2

0

One is

"The nearest ancestor in the view controller hierarchy that is a navigation Controller"

self.navigationController!.navigationItem.title = "test2"

enter image description here

The second type:

"The navigation item used to represent the view controller in a parent's navigation bar" self.navigationItem.title = "test"

enter image description here

Akshansh Thakur
  • 5,163
  • 2
  • 21
  • 38
0

The point is that all UIViewControllers have a UINavigationItem.

A UINavigationController decides what to show in its navigationBar by looking at the current viewController's navigationItem.

So you get this;

vcA.navigationItem.title = "A"
vcB.navigationItem.title = "B"

navigationController = UINavigationController(rootViewController: vcA)
// The title in the navigationBar is now "A"

navigationController.pushViewController(vcB, animated: true)
// The title in the navigationBar is now "B"

Since UINavigationController is also a subclass of UIViewController, it inherits the navigationItem as well, even though it's useless in most cases.

SpacyRicochet
  • 2,269
  • 2
  • 24
  • 39
  • Would that be the same for toolBar? so uiviewController has a toolBar, and uINavigationController has toolBar – sarah May 27 '16 at 10:16