0

I have searched everywhere and tried lots of code but nothing seems to be working for me. All I need to do is to load (on viewDidLoad) a text field and save it when a button is pressed.

What is the easiest method of doing this? I am working with a single window application, I don't have a view controller (this may make a difference?)

Thanks,

James

gcamp
  • 14,622
  • 4
  • 54
  • 85
pixelbitlabs
  • 1,934
  • 6
  • 36
  • 65
  • You say 'save'. What do you mean by save? Do you need to keep this data permanently? – James Webster Sep 03 '11 at 08:08
  • It's like a note taking app and I need users to be able to save the text fields when they close and open the app automatically without them having to do anything. – pixelbitlabs Sep 03 '11 at 08:25
  • And I got this error code with one recommended method of doing it: http://pastie.org/2475070 I followed this tutorial: http://twitc.com/Pkon7swrQ – pixelbitlabs Sep 03 '11 at 08:26

3 Answers3

1

Create a NSMutableDictionary as property and ...

when your button is clicked:

-(IBAction)buttonClicked:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];   
NSString *devicePath = [documentsDirectory stringByAppendingPathComponent:@"yourfile.txt"]; 
[self.dictionary setObject:self.textField.Text key:@"textField"];
[self.dictionary writeToFile:devicePath atomically:YES];
}

On your viewDidLoad, you can get the value of the file by:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"youefile"
                                                     ofType:@"txt"];
self.dictioary = [[[NSMutableDictionary alloc] initWithContentsOfFile:filePath] autorelease];
Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
Kenny Lim
  • 1,193
  • 1
  • 11
  • 31
1

Using an SQLite database:

-(IBAction) checkNotes{

sqlite3_stmt *statement;

const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
    NSString *querySQL = [NSString stringWithFormat: @"SELECT Notes FROM NotesTable WHERE UserID = (\"%@\")", userID.text]; 

    const char *query_stmt = [querySQL UTF8String];

    sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL);

    if (sqlite3_step(statement) == SQLITE_ROW) {

        NSString *notesField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];

        Notes.text = notesField;

        [notesField release];

    }else{
        Status.text = @"Not found";

    }

    sqlite3_finalize(statement);
}
sqlite3_close(contactDB);    

}

You can play around a little bit and adapt this code to your needs. For implementing SQLite3, check the net. But it will help you much in the future I guess. I think it would be the most flexible, because it will also allow you to create relational databases.

html_programmer
  • 18,126
  • 18
  • 85
  • 158
  • Is this difficult? How long would it take to do this? – pixelbitlabs Sep 03 '11 at 11:57
  • I forgot to mention: For the save: Use an "UPDATE" statement and check on SQLITE_DONE in this case. The method mentioned above is the one I would put in the viewDidLoad method. For initializing the database in your project, there is plenty of information on the internet. – html_programmer Sep 03 '11 at 11:57
  • Not very long, trust me - you'll get it before the end of the day; it's quite easy in fact. Here you have a tutorial on a View Based application: http://www.techotopia.com/index.php/An_Example_SQLite_based_iPhone_Application But I also suggest that you look up more websites in case you didn't immediately understand some details. – html_programmer Sep 03 '11 at 12:01
  • It seems kind of hard. So how would I save the data into the database, because you've given me the ability to load above? :-) – pixelbitlabs Sep 03 '11 at 12:05
  • Also, this is the tutorial I am trying to follow (which won't work at all for me as the data won't load): http://mobileorchard.com/how-to-make-an-iphone-app-part-6-saving-data/ – pixelbitlabs Sep 03 '11 at 12:06
  • P.S. I'm following this (http://www.techotopia.com/index.php/An_Example_SQLite_based_iPhone_Application) and I can't find the file "libsqlite3.dylib" in the Frameworks folder? – pixelbitlabs Sep 03 '11 at 12:12
  • And there's hundreds of errors when I add "#import "/usr/include/sqlite3.h"" to the .h file... – pixelbitlabs Sep 03 '11 at 12:14
  • Sorry man, I was out to the shop. If you type "libsqlite3.dylib" in the finder, you will find it back; in case you see multiple versions appear (which it will), look for the largest one (about 1,1MB size) or it won't work. Try also: #import "sqlite3.h" without the rest of the path. Your hundreds of errors will disappear :-) – html_programmer Sep 03 '11 at 12:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3123/discussion-between-user917467-and-james-anderson) – html_programmer Sep 03 '11 at 12:58
  • OK, just out at the shops too now ;) I'll chat with you later tonight. Thanks for your help! :D – pixelbitlabs Sep 03 '11 at 14:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3129/discussion-between-james-anderson-and-user917467) – pixelbitlabs Sep 03 '11 at 16:20
  • this is waaaaaaay too complicated for such a simple task – Radu Simionescu Mar 24 '14 at 09:32
0

Use the NSUserDefaults class which was specifically made for this scenario.

NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];

// saving a NSString (you usually do this when the value is being submitted)
[defs setObject:textField.text forKey:@"aKey"];
[defs synchronize]; //this commits the new value - shouldn't be necessary because this is doe automatically, but just in case...

// loading a NSString (you usually do this on viewDidLoad)
testField.text = [defs objectForKey:@"aKey"];

The value is stored across sessions.
You can store other value types as well (other than NSString).
Use a different key for each field for which you want to store values.
This can also be used to store app configurations/options/etc

Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
Radu Simionescu
  • 4,518
  • 1
  • 35
  • 34