title: How to add UIBarButtonItem to NavigationBar while using UIPageViewController
body: <p>Please forgive me, there are already a ton of questions on how to add a UIBarButtonItem to a NavigationBar programmatically but I just can't seem to get any of the solutions to work in my situation.</p>
  
  <p>Here is the layout of a simple test app I have been working with to learn the UIPageViewController.</p>
  
  <p><img src="https://i1290.photobucket.com/albums/b538/7eleven3/ScreenShot2014-02-07at64412PM_zpsa64a19f6.png" alt="storyboard"></p>
  
  <p>I have the page view controller working nicely with three unique viewControllers.  I would now like to set unique <code>rightBarButtonItems</code> for each of the view controllers.  I can easily set a common <code>barButtonItem</code> in the <code>DetailViewController</code> by simply doing this.</p>
  
  <pre><code>UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:@"Whatever" 
                                style:UIBarButtonItemStylePlain 
                                target:self 
                                action:@selector(doSomething)];    
  
  self.navigationItem.rightBarButtonItem = newButton;
  </code></pre>
  
  <p>Given that I need a unique button for each of the three pages in the <code>pageViewController</code> I am not able to set the button in the <code>detailViewController</code>.  I have tried the code above in  <code>viewDidAppear</code> of the three controllers but the buttons will not appear.  I have also tried creating the button in the same way and then setting it like this with no luck.   </p>
  
  <pre><code>DetailViewController *vc = [[DetailViewController alloc] init];
  vc.navigationItem.rightBarButtonItem = newButton;
  </code></pre>
  
  <p>I know I'm close here, I'm just not sure how to specify I need to add a button to the NavigationBar of the NavigationController that is running the detailViewController that my contentViewControllers are embedded in.  Any help would be great ESPECIALLY SNIPPETS.</p>
  
  <p>As a side note I have tried a few other methods that have come up problematic.</p>
  
  <ul>
  <li>Setting the button in <code>viewDidLoad</code> rather than <code>viewDidAppear</code>
  
  <ul>
  <li>This will not work because the <code>viewDidLoad</code> method is not called everytime you swipe from one page to the next.  The <code>viewDidAppear</code> method is however so I am trying to set the button there.  It is possible <code>viewWillAppear</code> is called everytime but I haven't checked.</li>
  </ul></li>
  <li>Setting the button in the UIPageViewController delegate methods
  
  <ul>
  <li>This is problematic if the user flips through the pages to quickly the button will fail to change or fail to appear.  </li>
  </ul></li>
  </ul>
  
  <p><strong><em>SOLUTION</em></strong></p>
  
  <p>It turns out I was close… Rather than creating the button in the UIPageViewController methods I simply needed to create a <code>navigationItem</code> property in each of my three view controllers.  When the controllers are instantiated in the delegate methods I simply set the navigationItem property equal to that of the detailViewController.  That way in my <code>viewDidApper</code> methods I could create the button.  Then at <code>viewWillDisappear</code> I set the button to <code>nil</code>.  You can also just create the button at <code>viewdidLoad</code> of the <code>DetailViewController</code> and change the text and action in <code>viewDidAppear</code> of the individual viewControllers.  Here is a sample.</p>
  
  <p>In the delegate methods</p>
  
  <pre><code>- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
  
  - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
  </code></pre>
  
  <p>I call a helper method based on the index of the viewController.  The helper method instantiates the view controllers when it is called.  Once instantiated I set some properties including the navigationItem property.</p>
  
  <pre><code>-(FirstController *)controllerAtIndex:(NSUInteger)index
  {
  
      FirstController *fvc = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstPageController"];
      fvc.imageFile = self.pageImages[index];
      fvc.titleText = self.pageTitles[index];
      fvc.pageIndex = index;
      fvc.navItem = self.navigationItem;
      return fvc;
  
  }
  </code></pre>
  
  <p>Then in the <code>viewDidAppear</code> of the viewController I just do this</p>
  
  <pre><code>-(void)viewDidAppear:(BOOL)animated
  {
  
      UIBarButtonItem *newButton = [[UIBarButtonItem alloc]
                                 initWithTitle:@"Whatever" style:UIBarButtonItemStylePlain target:self action:@selector(doSomething)];
      navItem.rightBarButtonItem = newButton;
  
  }
  </code></pre>