Change title of UINavigationController when UIPageViewController changes

Viewed 3866

I have a storyboard in Xcode5 where I have a UITabbarController that has a UINavigationController which is linked to a UIViewController. From that last UIViewController the rest (UIPageviewController and UITableViewController is created programmatically in the respective classes. See the screenshot.

Storyboard image of viewcontrollers

The problem is setting the title of the UINavigationController when the page of the UIPageViewController changes. I want the title to reflect a @property on the UITableViewController class but I am not sure how to accomplish this.

I have already tried using an NSNotification set in the UITableViewController but there is no method which gets called every time the user swipes to left or right for another page (when using the scroll type). I guess if I have a place to put the NSNotification so that it gets called every time the users changes the page it solves my problem.

Hopefully someone can give me a pointer where to look or what method or delegate to use.

2 Answers

The above solution was not working for me all the time. So an alternative solution could be using delegate,

protocol ChangeTitle {
    func changeTitle(title: String)
}

In the PagerController implement the protocol,

class MyPageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, ChangeTitle {
func changeTitle(title: String) {
        self.navigationItem.title = title
    }
}

In the individual view controller of pagerviewcontroller,

var changeTitle: ChangeTitle?

override func viewDidAppear(_ animated: Bool) {
    changeTitle?.changeTitle(title: "whatever the title is")
}

You also have to assign changeTitle variable where you are creating the viewcontroller in pagerviewcontroller.

Related