textfield value not getting saved to nsuserdefaults

Discussion in 'Mac Programming' started by abcdefg12345, May 7, 2014.

  1. abcdefg12345 macrumors 6502

    abcdefg12345

    Joined:
    Jul 10, 2013
    #1
    I'm trying to have a textfield that determines the number of decimals an app when calculating values. the problem is its not saving my the textfield value

    .h
    Code:
    #import <Foundation/Foundation.h>
    
    @interface Preferences : NSObject
    
    @property (weak) IBOutlet NSTextField *DecimalsField;
    -(IBAction) AddDecimal:(id)sender;
    -(IBAction) SubtractDecimal:(id)sender;
    
    @end
    .m
    Code:
    #import "Preferences.h"
    
    @implementation Preferences
    
    #define kDecimalsTitle @"DecimalsTitle"
    @synthesize DecimalsField = _DecimalsField;
    
    +(void) initialize
    {
        NSDictionary *defaults = [NSDictionary dictionaryWithObject:@"DecimalsTitle" forKey:kDecimalsTitle];
        [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
    }
    
    -(void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        [_DecimalsField setStringValue:[[NSUserDefaults standardUserDefaults] objectForKey:kDecimalsTitle]];
    }
    
    -(void)applicationWillTerminate:(NSNotification *)aNotification
    {
        [[NSUserDefaults standardUserDefaults] setObject:[_DecimalsField stringValue] forKey:kDecimalsTitle];
    }
    
    - (IBAction)AddDecimal:(id)sender
    {
        if ([_DecimalsField.stringValue isEqualToString:@"15"]) {
            return;
        } else {
            float result = [_DecimalsField intValue] + 1;
            [_DecimalsField setIntValue:result];
        }
    }
    
    - (IBAction)SubtractDecimal:(id)sender
    {
        if ([_DecimalsField.stringValue isEqualToString:@"0"]) {
            return;
        }else {
            float result = [_DecimalsField intValue] - 1;
            [_DecimalsField setIntValue:result];
        }
    }
    @end
    the code docent show any errors or warnings but it still docent save my values what have i done wrong
     
  2. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #2
    Save yourself some coding, debugging and pain by using Bindings. You can connect the text field in the .xib directly to UserDefaults this way and the loading and saving will be taken care of without any other effort on your part.
     
  3. briloronmacrumo macrumors 6502

    briloronmacrumo

    Joined:
    Jan 25, 2008
    Location:
    USA
    #3
    Several opportunies for improvement...

    applicationDidFinishLaunching: seems to set the field. Don't see where the "Preferences" object is allocated in the posted code. applicationDidFinishLaunching: seems to be in the Preferences object and not the App Delegate.

    Have you tried putting in some breakpoints and walking through it in the debugger? Can't diagnose without more code.

    FWIW: the *number* of decimals is presumably an integer but noticed the code assigns integer values to floats. Probably not a root cause.
     
  4. abcdefg12345 thread starter macrumors 6502

    abcdefg12345

    Joined:
    Jul 10, 2013
    #4
    I connected the object to File's owner and it worked for me
     
  5. mjohnson1212 macrumors member

    Joined:
    Nov 15, 2007
    #5
    You have to call synchronize on NSUserDefaults after you set the value for it to store.
     
  6. briloronmacrumo macrumors 6502

    briloronmacrumo

    Joined:
    Jan 25, 2008
    Location:
    USA
    #6
    Calling synchronize not required

    It is certainly possible and in some cases desirable to call synchronize but it isn't required. As Apple's docs note, synchronize is "automatically invoked at periodic intervals". See Apple's docs for the synchronize method and discussion.
     

Share This Page