Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SimonBS

macrumors regular
Original poster
Dec 30, 2009
202
0
Hi,

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

Code:
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.

When printing self.navigationController using NSLog in the parent view, I get a valid value but when doing so in the subview, it returns null. Is there anyway to "send" the navigationController to the subview?
 
super refers to the superclass in the inheritance tree (as it always does). It has nothing to do with the view hierarchy, in particular it does not refer to the superview/containing view. The predictably named superview property does however.
 
super refers to the superclass in the inheritance tree (as it always does). It has nothing to do with the view hierarchy, in particular it does not refer to the superview/containing view. The predictably named superview property does however.

Oh, I see. Now I know that. Thank you :)

I created the topic on StackOverflow as well and Mike gave me the following solution which works perfectly:
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.
 
Hmm, why can't you do something like this (typed straight into here, may not work or even compile):

Code:
UIView *viewWithNav = nil;
UIView *currentView = self.view;
while (viewWithNav == nil && currentView != nil)
{
  if (currentView.navigationController != nil)
  {
    viewWithNav = currentView;
  }
  else
  {
    currentView = currentView.superview;
  }
}
if (viewWithNav == nil)
{
  NSLog(@"Oops: cannot find a view with a navigation controller");
}
else
{
  [viewWithNav.navigationController pushViewController:bandDetailViewController animated:YES];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.