title: How to detect if a UIViewController has a back button
body: <p>I have a category that extends the functionality of a UIViewController that adds its own subtitle to the title bar. It needs to know what buttons are present in the title bar so that it can resize the labels within. I can detect if there is a leftBarButtonItem and rightBarButtonItem, but when it comes to a backBarButtonItem everything I have tried tells me that there is no back button, when in fact there is one when the view loads. This is what I've used to test:</p>
  
  <pre><code>if(self.parentViewController.navigationItem.backBarButtonItem == nil){
      NSLog(@"no back button");
  }
  else {
      NSLog(@"has back button");
  }
  
  if(self.parentViewController.navigationController.navigationItem.backBarButtonItem == nil){
      NSLog(@"1no back button");
  }
  else {
      NSLog(@"1has back button");
  }
  
  if(self.navigationItem.backBarButtonItem == nil){
      NSLog(@"2no back button");
  }
  else {
      NSLog(@"2has back button");
  }
  
  if(self.navigationController.navigationItem.backBarButtonItem == nil){
      NSLog(@"3no back button");
  }
  else {
      NSLog(@"3has back button");
  }
  
  if(self.presentingViewController.navigationItem.backBarButtonItem == nil){
      NSLog(@"4no back button");
  }
  else {
      NSLog(@"4has back button");
  }
  
  if(self.presentingViewController.navigationController.navigationItem.backBarButtonItem == nil){
      NSLog(@"5no back button");
  }
  else {
      NSLog(@"5has back button");
  }
  
  if(self.presentedViewController.navigationItem.backBarButtonItem == nil){
      NSLog(@"6no back button");
  }
  else {
      NSLog(@"6has back button");
  }
  
  if(self.presentedViewController.navigationController.navigationItem.backBarButtonItem == nil){
      NSLog(@"7no back button");
  }
  else {
      NSLog(@"7has back button");
  }
  </code></pre>
  
  <p>I've tried putting this in viewDidLoad, viewWillAppear and viewDidAppear, and they all return that there is no back button. In the previous view I do set the back button manually using <code>self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];</code> (The back button has to say back instead of the previous views title). Logically to me this means that the self.parentViewController is the one that should tell me if there is a back button on this view but clearly it doesn't.</p>