0

I have a navigation bar with tableview, when I choose one of the cells of tableview it leads me to another table view with title of the cell chosen, and also I use page view controller for the these tableviews.

I set title of navigation bar for the root table in viewcontroller of the first tableview as:

override func viewDidLoad() {
   self.navigationItem.title = "TableView Title"
}

I set the title of bar after choose the cell in the view controller of second tableview as:

override func viewDidLoad() {
    self.navigationController?.navigationBar.topItem?.title = "chosen cell  text"

}

override func viewDidAppear(animated: Bool) {
    self.navigationController?.navigationBar.topItem?.title = "chosen cell  text"
}

With these codes, it sets root tableview's title properly.

But when I choose a cell. root table's title is appear in back button. And there is no other title.

When I go next page of second tableview, the title of the second tableview appear properly. Therefore why the second tableview's title doesnot appear, when it first appears. How can I invoke it?

Solution: Firstly thanks for your help. I try to change the title in page viewer controller by self.navigationItem.titleView = "chosen cell text" and it works.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
sof98789
  • 1,051
  • 11
  • 17

1 Answers1

1

Set the title property of the Child View Controller itself.

Example:

override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "Chosen Cell Text"
}
Philip Hardy
  • 196
  • 2
  • 9
  • child viewcontroller doesnot have its own bar. So it does not change anything – sof98789 Sep 19 '15 at 22:16
  • Correct, the view controllers should share the navigation controller and therefore the navigation bar. However, the navigation controller automatically updates the navigation bar by reading the view controller's title property. This code is tested. Trust me, I know it works. Get rid of the lines where you are manually setting the nav bar title. – Philip Hardy Sep 19 '15 at 22:22
  • but it doesnt work for me all I see is "root view controller". maybe the problem is page viewer controller that take cares display of second table. – sof98789 Sep 19 '15 at 22:29
  • How are you displaying the child view controller? Are you creating it then pushing it onto the Navigation Controller stack? – Philip Hardy Sep 19 '15 at 22:34
  • I am new in ios development so I am not sure technically. But the root table controller is linked with page view controller, that handles the display of the second table as pages. – sof98789 Sep 19 '15 at 22:43
  • I'm going to have to see more of your code to understand. I know with my code, I always first embed a view controller into a navigation controller. Then I create a new child view controller when needed and push it onto the navigation controller stack in order to display it using `self.navigationController?.pushViewController(childViewController, animated: true)` – Philip Hardy Sep 20 '15 at 03:57