2

I have two text box ,I want to store the data of this text box into the Plist when i Press submit. so far i gone through the code to write ,but problem is how to write data of the text box?? like I have textbox1 and textbox2 .want to store data into plist

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Comments" ofType:@"plist"];
NSMutableArray *plistArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];


NSMutableDictionary *newComment = [NSMutableDictionary dictionary];
[newComment setValue:commentTitle.text forKey:@"title"];
[newComment setValue:comment forKey:@"comment"];


[plistArray addObject:newComment];
[plistArray writeToFile:filePath atomically:NO];

please suggest me the correct way

Venk
  • 5,949
  • 9
  • 41
  • 52
Christien
  • 1,213
  • 4
  • 18
  • 32

2 Answers2

3

Let your textboxes be textbox1 and textbox2

- (IBAction) saveData
{
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Comments.plist"];

// set the variables to the values in the text fields
self.title = textbox1.text;
self.comment = textbox2.text;



// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: title, comment, nil] forKeys:[NSArray arrayWithObjects: @"title ", @"comment", nil]];

NSString *error = nil;
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

// check is plistData exists
if(plistData) 
{
    // write plistData to our Data.plist file
    [plistData writeToFile:plistPath atomically:YES];
}
else 
{
    NSLog(@"Error in saveData: %@", error);
    [error release];
  }
 }
Rose
  • 437
  • 2
  • 9
  • thanks! what would be the yourkeytostore1 and yourkeytostore2?? – Christien Dec 06 '12 at 06:41
  • sorry but there is a problem ,I have a plist with key=title and value =comment and two textboxs. now self.title = textbox1.text;...whats that title would would be?.. i just copied the question code from somewhere... – Christien Dec 06 '12 at 06:48
  • @property (nonatomic, retain)NSString *title,@property (nonatomic, retain)NSString *comment,these are two strings to which the values are stored – Rose Dec 06 '12 at 06:52
  • i did the same thing .I created a blank plist , everything same what you said..but I can see any changes inn my plist :( – Christien Dec 06 '12 at 07:10
  • maybe .... you must check if plist exist in Documents Directory.. if not .. copy to it. – TonyMkenu Dec 06 '12 at 09:56
  • @Christien were you check your plist? in Bundle or in Documents Directory? – TonyMkenu Dec 06 '12 at 09:58
  • @TonyMkenu YA this code is working and storing in document directory..but each time it enter it overwrites...i want code to register... – Christien Dec 06 '12 at 10:35
  • ahaaa :) With a simple command you can't ..But: 1) you must read the existing plist 2) create an array.. 3) add the new data to array... and finally .. write the data from array the your plist – TonyMkenu Dec 06 '12 at 10:42
  • you have to "combine" the @Rose code (//create dictionary with values in UITextFields).. with your "reading" from plist code.. The "final" NSDictionary *plistDict.. will be written to the Document Directory – TonyMkenu Dec 06 '12 at 11:15
  • here a similar question http://stackoverflow.com/questions/13740103/writing-textbox-data-to-plist – TonyMkenu Dec 06 '12 at 12:14
  • search plist problem..help http://stackoverflow.com/questions/13985379/uisearchview-not-displaying-search-values – Christien Dec 21 '12 at 07:41
1

Create an array in plist named as title , then do liket this,

-(void) SubmitAction {
    NSString *path = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *finalPath = [path stringByAppendingPathComponent:@"your_plist_name"];
    NSMutableDictionary *plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];
    NSMutableArray *titleArray=[plistDict valueForKey:@"title"];

    [titleArray addObject:textbox1.text];

    [plistDict setValue:titleArray forKey:@"title"];

    [plistDict writeToFile:finalPath atomically:NO];
}

try with one textbox first .....sure it will work....

Venk
  • 5,949
  • 9
  • 41
  • 52