Saving a Text

Discussion in 'iOS Programming' started by Programmer, Sep 28, 2009.

  1. Programmer macrumors member

    Programmer

    Joined:
    Jun 16, 2009
    #1
    How can you Save and read information in your application such as a high score.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Do you mean between sessions of your app running? If so, consult your thread about NSUserDefaults, particularly the post by Macdonaldsd.
     
  3. Programmer thread starter macrumors member

    Programmer

    Joined:
    Jun 16, 2009
    #3
    yes

    yes. I tried that but for some it didn't save the text.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    What is going wrong with it? Do you have a code snippet we can look at?
     
  5. Programmer thread starter macrumors member

    Programmer

    Joined:
    Jun 16, 2009
    #5
    Heres the code

    I tried a simple test to see if it worked

    Code:
    - (void)viewDidLoad {
    		     [[NSUserDefaults standardUserDefaults] setObject:@"Name" forKey:@"Text"];
        [super viewDidLoad];
    }
    
    
    - (IBAction)SetTextFieldToSavedText {
    	
    	NSString *name = [ [NSUserDefaults standardUserDefaults] stringForKey:@"Text"];
    	
    	names.text = name;
    	
    }
    
    - (IBAction)PersonEndedTyping {
    	
    	[[NSUserDefaults standardUserDefaults] setObject:names forKey:@"Text"];
    
    }
    The problem is that what it told it ti save isn't saving.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Here's your problem:
    names is a UITextField object, not an NSString. What you are doing is setting the UITextField object for that key in NSUserDefaults. What you want to do is set the value of the text property of the UITextField. Like this:
    Code:
    [[NSUserDefaults standardUserDefaults] setObject:names[B].text[/B] forKey:@"Text"];
     
  7. Programmer thread starter macrumors member

    Programmer

    Joined:
    Jun 16, 2009
    #7
    Thanks So Much!

    Thanks a lot. By the way is that app CraigsHarvest your advertising under your comments yours?
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    It is. There's even a whole thread about it: Announcing CraigsHarvest
     
  9. Programmer thread starter macrumors member

    Programmer

    Joined:
    Jun 16, 2009
    #9
    I Bought

    I Bought it i really like your interface and how smoothly it runs
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Thank you. We appreciate the comments.
     
  11. Programmer thread starter macrumors member

    Programmer

    Joined:
    Jun 16, 2009

Share This Page