Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change title of UINavigationController when UIPageViewController changes

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.

like image 337
Hugo Avatar asked Jan 02 '14 17:01

Hugo


1 Answers

I had to do exactly this in the app I'm currently working on.

First ensure you have set self.pageViewController.delegate to self.

Here's what I did:

// Notice when the pageviewcontroller finishes animating between pages.
- (void)pageViewController:(UIPageViewController *)pageViewController
    didFinishAnimating:(BOOL)finished
   previousViewControllers:(NSArray *)previousViewControllers
   transitionCompleted:(BOOL)completed {

  // .viewControllers[0] is always (in my case at least) the 'current' viewController.
  UIViewController* vc = self.pageViewController.viewControllers[0];
  self.navigationItem.title = vc.navigationItem.title;
}

The 'secret' is that (at least in the simple swipe-left-right case with full-screen sized pages I have) that self.pageViewController.viewControllers[0] is the 'current' page.

To use your property rather than the navigationItem title, then just change the last line to something like self.navigationItem.title = ((MyViewController*)vc).myProperty;

like image 54
Ben Clayton Avatar answered Nov 19 '22 09:11

Ben Clayton