Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parent view navigationController

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.

like image 591
simonbs Avatar asked Feb 20 '11 16:02

simonbs


People also ask

How do I get Viewcontroller from view?

If you need to find the view controller that is responsible for a particular view, the easiest thing to do is walk the responder chain. This chain is built into all iOS apps, and lets you walk from one view up to its parent view, its grandparent view, and so on, until it reaches a view controller.

How to get current view controller in navigation controller?

Accessing the Root View Controller But it's easy to do. The root view controller is simply the view controller that sits at the bottom of the navigation stack. You can access the navigation controller's array of view controllers through its viewControllers property.

What is a UIViewController?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.


1 Answers

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];
like image 70
Rayfleck Avatar answered Sep 19 '22 09:09

Rayfleck