views:

285

answers:

1

Hey all,

I have a basic question that's driving me crazy.

I have a class inheriting from UITableViewController. It is the root controller in my navigation controller. I have another class that implements the UITableViewDelegate and UITableViewDataSource protocols (my delegate).

Basically, in my delegate's tableView:didSelectRowAtIndexPath: method, I want to push a new controller.

When the delegate protocols were simply implemented by the UITableViewController, it was easy:

[self.navigationController pushViewController:nextController animated:YES];

I (obviously) can't do that in the delegate class. How the heck do I refer to the UITableViewController subclass to get access to the navigationController? It's driving me nuts!


Basic Flow:

  1. UITableViewController calls delegate
  2. delegate runs tableView:didSelectRowAtIndexPath:
  3. in the delegate's tableView:didSelectRowAtIndexPath:, code needs to push new controller onto navigationController
  4. delegate does not have reference to navController or the TableViewController to do the push.
  5. code gives a compiler error if i try to pass into the delegate the tableController or navController via a property. so, i can't give it a reference back to the calling tableViewController
A: 

Your UIApplicationDelegate should have a reference to the navigation controller. You don't actually need a reference to the UITableViewController.

You can simply do something like this:

// Get the App delegate
YourAppDelegate * app = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];

// Get the UINavigationController used by the application
UINavigationController * nav = app.navigationController;

// Push the next view controller
[nav pushViewController:nextController animated:YES];
Ben S
*smacks forehead*I knew I was overlooking something easy! I'm coming in from Java, so some of hierarchy on how things are put together is still fuzzy :)Thanks!
John