3

我有带有文本字段和 2 个按钮的警报窗口,我需要将在我的文本字段中添加的文本保存到 .plist 文件,我该怎么做?

.h 文件

NSMutableDictionary *cameras;

我的警报代码

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == [alertView firstOtherButtonIndex])
    {
        NSString *plistPath = [DOCUMENTS stringByAppendingPathComponent:@"Cameras.plist"];
        NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:cameras format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];
        if(plistData)
            [plistData writeToFile:plistPath atomically:YES];
    }
}
4

1 回答 1

2

text从文本字段中获取。创建一个NSDictionary,并写入文件。

UITextField *textfield = [alertView textFieldAtIndex:0];
NSString *value =   [textfield text];
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:value forKeys:@"key"];
[dictionary writeToFile:path atomically:YES];
于 2013-08-13T14:38:13.443 回答