views:

82

answers:

2

Hi,

I am able to pass a variable forward from view controller to view controller by pushing its view controller onto the navigation stack. An example of how I do it would be this:

MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
controller.myString = stringToPass;
[self.navigationController pushViewController:controller animated:YES];
[controller release];

However, what do I do if I want to pass a variable BACK UP the navigation stack? Using popViewControllerAnimated rather than pushViewController does not pass the variable up like I thought it would.

I need to be able to access the variable several pops up from the view controller it is defined in.

Any help will be greatly appreciated :)

A: 

You can get a hold of the navigation controller in the VC stack using self.navigationController. You can just call some method like

[self.navigationController setMyString:stringToPassUp];

There are several more ways, e.g. self.tabBarController for the tabbarcontroller up in the stack, or most simply

[self.parentViewController setMyString:stringToPassUp];

edit given the downvotes on the examples above, and nobody giving a better explanation, let's discuss the proper way to do this.

If you have some object (like your MyViewController *controller) and that object has something to tell you, the usual approach is this:

  • MyViewController gets a delegate property, of type (id)
  • the view controller instantiating the MyViewController, sets this delegate property, like so:
    controller.delegate = self;
  • MyViewController will, when it has something to say, do something like:
    [self.delegate delegateMessage:arg1]; to "pass the message up" as you put it.

To do it perfectly, you may want to create your own @protocol MyViewControllerDelegate, and declare the class which would be setting controller.delegate = self; to adopt this protocol, by putting <MyViewControllerDelegate> on the @interface line. The delegate property of MyViewController should then be declared id<MyViewControllerDelegate> delegate;, so that the [self.delegate ...] messages can be matched to the protocol specification.

Basically the whole Cocoa Touch API works like this. Just have a look around for ideas how to implement your interaction.

mvds
What's with the downvotes?!
mvds
They're not mine, but I presume they're because you're promoting some wonky flow-control ideas.
Seamus Campbell
;-) just answering the question...
mvds
Thanks for the help. In the end I decided to use my app delegate as that seemed the simplest (and most common) solution.
Martyn
one day, you will regret that ;-)
mvds
A: 

You're passing values, not variables.

A view controller should not be responsible for popping itself. With Apple's view controllers (e.g. UIImagePicker), it is the parent view controller's responsibility to do the popping; the parent VC can also obtain the current value. (Not entirely correct; it might access the value before a keyboard autocompletion is applied)

Alternatively, if it's a value that can be shared globally, you can store it in your application delegate.

tc.
Thank you for your help :)
Martyn