Reputation: 8042
I have a view which is a part of a tabBarController. In this view I have a subview with a table. When clicking a cell in this table, I would like to access the navigationController of the parent view. Is this possible - and if so, how?
I thought it would be
BandDetailViewController *bandDetailViewController = [[BandDetailViewController alloc] initWithNibName:@"BandDetailViewController" bundle:nil];
bandDetailViewController.band = [thisDay objectAtIndex:indexPath.row];
[super.navigationController pushViewController:bandDetailViewController animated:YES];
[bandDetailViewController release];
But that does not work.
Upvotes: 8
Views: 7628
Reputation: 12106
When you instantiate your sub-view controller, pass in a reference to the navigation controller (super's), and store it in an instance variable. You can then reference it when you need it in the sub. I have been looking for a more elegant solution to this and similar problems, without success. Passing in a reference works, so I do that and try to get on with my life.
EDIT: add code sample
In mainVC.m
// this might be in didSelectRowForIndexPath:
SubViewController *subVC = [[SubViewController alloc] init];
subVC.superNavController = self.navController;
[self.navigationController pushViewController:subVC animated:YES];
In SubViewController.h
@property (nonatomic, retain) UINavigationController *superNavController;
In SubViewController.m
@synthesize superNavController;
// then, wherever you need it, say to pass to a sub-sub-viewController...
[self.superNavController pushViewController:myNewVC animated:YES];
Upvotes: 10
Reputation: 5432
When you call -pushViewController, which view controller is self? If you are calling that from within one of your tab subviews, self likely doesn't have a reference to the navigation controller from the top level view that you added it to.
Please have a look at this post. This guy looks like having same problem.
Unable to pushViewController for subview
Upvotes: 0