Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

parkov

macrumors newbie
Original poster
Aug 29, 2005
24
19
I have two views with respective controllers. An object is created in the first controller. The second view has some UI controls for settings. When a control value is changed in the second view, how do I set a new value for a property of an object instantiated in the first controller?

If I wanted to set a new value from within the first controller, I could use [self.object setProperty:newValue]. Can I set the new value from another controller? If not, what's the best approach?
 

keehun

macrumors regular
Mar 17, 2008
110
0
You connect it to IB, or get the first controller programmatically and instead of doing [self object] you do [firstController object]...
 

parkov

macrumors newbie
Original poster
Aug 29, 2005
24
19
Can you explain how to do it each of these ways? I understand that I can reference the object now, but what exactly needs to be done to be able to call it like that?
 

ppinter1

macrumors newbie
Sep 4, 2008
14
6
I'd like to know too...

When I last looked at accessing a variable between View Controllers (VC), I hit a wall.

In fact, I was fairly discouraged that either I was missing something or there was more to it than met the eye.

I read a posting somewhere to de-reference the target VC using the App Delegate. And I *almost* had it working, but no joy :confused:

Because it was iPhone app settings data I was trying to pass between VCs, I ended up using the persistent store of NSUserDefaults, using code like this to save the value when a TextField in one VC completed editing:

Code:
- (void)textFieldDidEndEditing:(UITextField *)theTextField {
	
	NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];

	[defs setObject:[NSString stringWithFormat:@"%1.1f", txtParameter1.text.floatValue] forKey:@"parameter1"];

	[defs synchronize];

	theTextField.text = [NSString stringWithFormat:@"%1.1f", theTextField.text.floatValue]; 
}

... and when the target VC is about to come into view, using code like this to read the setting into a variable:

Code:
- (void)viewWillAppear:(BOOL)animated {
	
	NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
	
	parameter1		= [[defs stringForKey:@"parameter1"] floatValue];

If you use this approach, be sure to test if the setting is already in persistent storage, and if not to set a default value. Here's what I did in my main VC initializer:

Code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		
		NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
		
		if ([defs stringForKey:@"parameter1"] == nil) [defs setObject:@"0.0" forKey:@"parameter1"];
.
.
.

If there is a better way to access variables between View Controllers, assuming it can be done at all, I'm sure both the OP and I would welcome some demo code, rather than generalized comments to show us how. Thanks very much.
 

Luke Redpath

macrumors 6502a
Nov 9, 2007
733
6
Colchester, UK
It makes sense that you can't access a variable in one object from another object unless object two has a reference to object one and the variable is public). You need to think about your design a bit more.

You could have a shared data store (a dictionary, for example) that you access from your app delegate but I find it's general to pass any object needed by another one into it, either in the constructor if it's needed throughout the whole class, or as a method argument if it's just needed in that one method. Finally, you could use accessors to pass dependent objects in but I would use that as a last resort if it it's not easy to create a custom constructor.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.