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.
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.
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 ();
homeScreen just contains a button which loads a new view containing a table
view (OrderList). Here's the button event handler.
void HandleOrdersButtonhandleTouchUpInside (object sender, EventArgs e)
{
if (orderListScreen == null)
orderListScreen = new OrderList();
NavigationController.PushViewController(orderListScreen, true);
}
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).
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);
}
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?
Can anyone tell me what I might be missing here?
Thanks in advance.
** An update after reviewing Jason's comment to same question on
Stackoverflow:
So, I tried the following:
I saved a reference to the NavigationController in the constructor for the
ViewController that contains the table view as follows:
public partial class OrderList : UIViewController
{
UINavigationController navController;
public OrderList () : base ("OrderList", null)
{
this.Title = "Orders";
navController = this.NavigationController;
}
Then passed that into the TableViewDelegate, where the
AccessoryButtonTapped is handled, in the ViewDidLoad method.
public override void ViewDidLoad ()
{
orderTableView.DataSource = new OrdersTableViewDataSource();
orderTableView.Delegate = new OrdersTableViewDelegate(navController);
base.ViewDidLoad ();
}
Then referenced that in the TableViewDelegate:
public class OrdersTableViewDelegate : UITableViewDelegate
{
UINavigationController navController;
public OrdersTableViewDelegate(UINavigationController controller)
{
navController = controller;
}
// Rest of class definition
}
Then the reference to the NavigationController using navController compiles
with the code as previously described using the following in the
AccessoryButtonTapped method:
navController.PushViewController(detailScreen, true);
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?
Cheers
_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch