views:

23

answers:

1

Hi

I am follwing a tutorial from a book and there the delegate and datasource are separated from the controller (MyViewController.m)

[self setDataSource:[[MyViewDataSource alloc]
[self setDelegate:[[MyViewDelegate alloc]

for understanding, I now want to pop a controller from the delegate class (MyViewDelegate.m)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        2ndViewController *controller = [[2ndViewController alloc]];
        [[self navController] pushViewController:controller animated:YES];

of course this will not work since the navcontroller sits in the app delegate. But how do I best access the navcontroller from the delegate class ?

+2  A: 

You can do something like

UINavigationController *navController = [(MyAppDelegate*)[[UIApplication sharedApplication] delegate] navigationController];

However, you should ask yourself why you need to do this and if there is a better way that is more in keeping with MVC (model view controller) and the rules of encapsulation.

For instance, UIViewController presents a property named navigationController, which, as explained by the documentation, will return the appropriate navigation controller for the given view controller.

Jasarien
Yes, if your delegate class is distinct from your view controller, and needs access to the nav controller, a better approach is to set a property on the delegate object when you initialize it.
Seamus Campbell
Thanks for the fast replies!The only reason I wanted to do it was to get it working by the tutorials way...
RRZ Europe