Val Nolav
Val Nolav

Reputation: 902

passing variable value between views and classes

I am trying to pass variable values between classes of different views. I am using navigation controller and passing views from one navigation controller to another is possible via declaring property and synthesizing.

However, I do not know how can I do that from one view to not NEXT view but other views in the stack.

For example,

FirstViewController -> SecondViewController -> ThirdViewController

This is the sequence of views in navigation controller.

I want to pass a variable value in FirstViewController to ThirdViewController.

How can I do that?

I tried in the FirstViewController something like that:

SecondViewController *second [SecondViewController alloc] init] autorelease];    
ThirdViewController *third = [ThirdViewController alloc] init] autorelease];  
third.passVariable = myVariable;

.....
[[self.navigationController pushViewController:second animated:YES];
....

How can I do that? can anyone provide me some example code lines?

Many thanks in advance..

Upvotes: 0

Views: 172

Answers (2)

Abizern
Abizern

Reputation: 150605

I presume it doesn't work because you are passing the variable to the third view controller, but pushing the second view controller. Although the third view controller is created, it is autoreleased before the second view controller can push the third view controller.

Your approach is correct, but what you should do is pass the variable to the second view controller, and when the second view controller creates the third view controller it can pass it to that.

Edited after comment

Delegation (which is where you use a protocol) is a way of getting information back to a controller. i.e. if you want to return information back to the first View controller from the third view controller. I wrote a simple example here. But, to set the first view controller as the delegate of the third view controller, you still need to pass a reference to the first view controller up the stack in the same way as I've described above.

If you want to see how to set up a singleton data source, create an iOS project with Core Data and see how the template sets up the managed object context.

Upvotes: 2

Amit Shah
Amit Shah

Reputation: 4164

UINavigationController has a property called viewControllers which is an NSArray of all the View Controllers in the Navigation Controller. You could use that to access other controllers.

So something like this might be what you need. Assuming your in the third View Controller, and you want a variable in the first. (And the First has a method to get the variable.

id obj = [[[navigationconroller viewControllers] objectAtIndex:0] getMyVar];

I haven't tested that so it might not work exactly. You may have to use [viewcontroller performSelector:@selector(getMyVar)]; where viewcontroller is the View Controller taken from your navigationcontroller.viewControllers.

Hope that makes sense.

Upvotes: -1

Related Questions