Parent view navigationController

Discussion in 'iOS Programming' started by SimonBS, Feb 20, 2011.

  1. SimonBS macrumors regular

    SimonBS

    Joined:
    Dec 30, 2009
    #1
    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?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. SimonBS thread starter macrumors regular

    SimonBS

    Joined:
    Dec 30, 2009
    #3
    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:
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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];
    }
    
     

Share This Page