views:

46

answers:

3

hi, I am working in xcode on an ipod app in objective C, and I have a field (navigationController) in one of my classes (rootViewController). How do I reference the instantiated rootViewController's navigationController from another class? For example, how would I do this if I want to reference the navigationController from the FirstViewController.m class?

I seem to only be able to find references for this to reference the application delegate.

If I want to reference the application delegate with FirstViewController, I just say:

MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];

[delegate.navigationController pushViewController:childController animated:YES];

how do I do this for the rootViewController class instead of MyAppDelegate?

A: 

One object needs a reference to the other. There are many ways to make this happen. For example:

  • Have the same object create both and, since it knows them both, tell them about each other
  • Create them in the same nib and hook them up with normal connections
  • Have the owner of the nib one of the objects is in know about the other

Essentially, this is just a matter of designing your application properly so that the objects can be told about each other. There is no one magic thing you do.

Chuck
A: 

One way to do this will be to pass in navigationController's reference to FirstViewController

For example, if you are instantiation FirstViewController in your code and navigationController is available in the scope, you can write:

FirstViewController *firstViewController = [[FirstViewController alloc]init];
[firstViewController setNavigationController:navigationController];

Ofcourse, you will have to update your FirstViewController to have a navigationController property

Mihir Mathuria
A: 

Based on the question you are asking, it seems that you want to call methods on the UINavigationController that is higher up in the stack. All UIViewControllers already have a property of navigationController which is the UINavigationController that is an ancestor to this ViewController on the stack.

So if you had a RootViewController, called root, that at some point did this

FirstViewController * first = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
[self.navigationController pushViewController:first animated:YES];
[first release];

Then first.navigationController == root.navigationController

So inside of first calling [self navigationController] will give you the same navigationController that first was pushed onto in the first place.

Brandon Bodnár
yep, this is exactly what I was asking. wow I was stuck on this for 2 hours and all I had to do was call [self.navigationController pushViewController...] . Thanks!
Paul