我有两个文本框,当我按下提交时,我想将此文本框的数据存储到 Plist 中。
到目前为止我已经完成了编写代码,但问题是如何编写文本框的数据?就像我有 textbox1 和 textbox2 一样。想将数据存储到 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];
请给我正确的方法
Best Answer-推荐答案 strong>
让你的文本框成为 textbox1 和 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];
}
}
关于iphone - 将文本框数据写入 Plist,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/13738054/
|