Setting the Value of an Object

Discussion in 'iOS Programming' started by parkov, Sep 3, 2008.

  1. parkov macrumors newbie

    Aug 29, 2005
    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?
  2. keehun macrumors regular

    Mar 17, 2008
    You connect it to IB, or get the first controller programmatically and instead of doing [self object] you do [firstController object]...
  3. parkov thread starter macrumors newbie

    Aug 29, 2005
    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?
  4. ppinter1 macrumors newbie

    Sep 4, 2008
    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:

    - (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:

    - (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:

    - (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.
  5. Luke Redpath macrumors 6502a

    Nov 9, 2007
    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.

Share This Page