title: Missing Reference to Navigation Controller in Monotouch
body: <p>I am new to Monotouch and attempting to understand how some of the basics hang together. Hopefully someone out there will be able to assist.</p>
  
  <p>I've created a test project in MonoDevelop based on the Multi-Screened Apps tutorial on the Xamarin site and have extended it to include a tableView. I am having issues with referencing the Navigation Controller in a view that I need to push a detail view onto to display the detail of an item tapped in the table via an accessory button. I know some of the coding is scrappy, just been trying to get it working at this stage rather than the clarity in the code! (I'm using the latest versions of all Mono tools/libraries etc and XCode 4 on Lion). Starting at the beginning here's the code in FinishedLaunching in AppDelegate. </p>
  
  <pre><code>window = new UIWindow (UIScreen.MainScreen.Bounds);
  this.rootNavigationController = new UINavigationController();
  // Create a new homescreen
  HomeScreen homeScreen = new HomeScreen();
  // Add the homescreen to the root navigation controller
  this.rootNavigationController.PushViewController(homeScreen, false);
  // Set the root view controller on the window - the navigation controller
  // will handle the rest.
  this.window.RootViewController = this.rootNavigationController;
  // make the window visible
  this.window.MakeKeyAndVisible ();
  </code></pre>
  
  <p>homeScreen just contains a button which loads a new view containing a table view (OrderList). Here's the button event handler.</p>
  
  <pre><code>void HandleOrdersButtonhandleTouchUpInside (object sender, EventArgs e)
  {
      if (orderListScreen == null)
          orderListScreen = new OrderList();
      NavigationController.PushViewController(orderListScreen, true);
  } 
  </code></pre>
  
  <p>This all works fine. I've got some dummy data that loads into the table view, which also works fine. OrderData is a simple class for testing which just contains a couple of properties. I've added an AccessoryButton to the cells and am trying to load a detail view when this is tapped. Here's the code that does this - comment in code where issue is! (I'd previously tested the AccessoryButtonTapped functionilty was working by just displaying an alert).</p>
  
  <pre><code>public override void AccessoryButtonTapped (UITableView tableView, NSIndexPath indexPath)
  {
      var dataSource = (OrdersTableViewDataSource)tableView.DataSource;
      if (detailScreen == null)
          detailScreen = new OrderDetailScreen();
      OrderData theOrder = dataSource.OrdersData[indexPath.Row];
      detailScreen.currentOrder = theOrder;
  
      // Cant get a reference to NavigationController here to push the detail view!
      // this.NavigationController is not available
      this.NavigationController.PushViewController(detailScreen, true);
  }
  </code></pre>
  
  <p>My understanding of NavigationControllers from what I've read so far is that this reference should be available through all views that originate from the root ViewController/NavigationController without the need to pass the reference from AppDelegate through the various view constructors?</p>
  
  <p>Can anyone tell me what I might be missing here?</p>
  
  <p>Thanks in advance.</p>
  
  <p>** An update after reviewing Jason's comment: (Please let me know if this is the incorrect way to post updates)</p>
  
  <p>So, I tried the following:</p>
  
  <p>I saved a reference to the NavigationController in the constructor for the ViewController that contains the table view as follows:</p>
  
  <pre><code>public partial class OrderList : UIViewController
  {       
      UINavigationController navController;
  
      public OrderList () : base ("OrderList", null)
      {
          this.Title = "Orders";
          navController = this.NavigationController;
      }
  </code></pre>
  
  <p>Then passed that into the TableViewDelegate, where the AccessoryButtonTapped is handled, in the ViewDidLoad method.</p>
  
  <pre><code>public override void ViewDidLoad ()
  {
      orderTableView.DataSource = new OrdersTableViewDataSource();
      orderTableView.Delegate = new OrdersTableViewDelegate(navController);
  
      base.ViewDidLoad ();
  }
  </code></pre>
  
  <p>Then referenced that in the TableViewDelegate:</p>
  
  <pre><code>public class OrdersTableViewDelegate : UITableViewDelegate
  {
      UINavigationController navController;
  
      public OrdersTableViewDelegate(UINavigationController controller)
      {
          navController = controller;
      }
  
      // Rest of class definition
  }
  </code></pre>
  
  <p>Then the reference to the NavigationController using navController compiles with the code as previously described using the following in the AccessoryButtonTapped method:</p>
  
  <pre><code>navController.PushViewController(detailScreen, true);
  </code></pre>
  
  <p>When I run this and tap on the AccessoryButton I get a null reference exception on navController. The reference to this.NavigationController in the ViewController constructor is null. Am I doing something in the wrong place or sequence?</p>
  
  <p>Cheers</p>