I am follwing a tutorial from a book and there the delegate and datasource are separated from the controller (MyViewController.m)
[self setDataSource:[[MyViewDa开发者_开发问答taSource 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 ?
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.
精彩评论