8

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.

simonbs
  • 7,932
  • 13
  • 69
  • 115

2 Answers2

10

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];
Rayfleck
  • 12,116
  • 8
  • 48
  • 74
0

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

Community
  • 1
  • 1
iHS
  • 5,372
  • 4
  • 31
  • 49
  • Thanks, but nothing happens when this is called. No error, no nothing. – simonbs Feb 20 '11 at 16:29
  • When reading your link it seems to me that it is not really possible to achieve what I am looking for, is that right? It is correct that my self.navigationController is `null` in the subview but has a valid value in the parent view. Is there no way to send the navigationController to the subview from the parent view? – simonbs Feb 20 '11 at 16:47
  • I am not sure about it, but there must be some way around. But alternatively, what you can do is, when you select a row i.e in `didSelectRowAt...`method, remove the subview from superview, and then your top most view should not ne tableview anymore. So then push the controller. I think it should work – iHS Feb 20 '11 at 17:01