title: Implementing a UIViewController category
body: <p>I'm trying to implement a category on my MainContainerViewController that returns the main instance:</p>
  
  <p>In my .h:</p>
  
  <pre><code>@interface UIViewController(MainViewExtension)
  /** Convience method for getting access to the MainContainerViewController instance */
  - (MainContainerViewController *)mainContainerExtension;
  @end
  </code></pre>
  
  <p>In my .m</p>
  
  <pre><code>@implementation UIViewController(MainViewExtension)
  - (MainContainerViewController *)mainContainerExtension
  {
      return (MainContainerViewController *)self;
  }
  @end
  </code></pre>
  
  <p>And so from some outside view controller, I do self.mainContainerExtension to access the instance, and there is only one instance declared, but it's not giving me the right instance. </p>
  
  <pre><code>NSLog(@"number of children:%i", [self.mainContainerExtension.childViewControllers count]);
  </code></pre>
  
  <p>This returns 0 even though it should be 3, so that's how I know it's not giving me the right results. Am I missing something?</p>
  
  <p><strong>Edit:</strong> The way I know it's not giving me the correct instance is because in the <code>viewDidLoad</code> of the <code>MainContainerViewController</code>, I have this:</p>
  
  <pre><code>- (void)viewDidLoad
  {
      appDelegate.notesViewController=[[NotesViewController alloc] init];
      appDelegate.notesViewController.mainContainer=self;
      [self addChildViewController: appDelegate.notesViewController];
  }
  </code></pre>
  
  <p>Each <code>NotesViewController</code> has a property of type <code>MainContainerViewController</code>, so that I always have a way to access the <code>MainContainerViewController</code> from <code>NotesViewControllers</code>. But I just learned about categories and extensions, and I thought it might be easier to just implement a category that gives me access to the container view from whichever view I'm in, rather than having a property on every view controller. It's my first time, so I'm sure I'm doing something wrong with my category implementation, just not sure what it is.</p>