如何将UIAlertView文本字段值保存到.plist文件?

时间:2013-08-13 14:35:41

标签: ios objective-c uitextfield plist uialertview

我有带文本字段和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];
    }
}

1 个答案:

答案 0 :(得分: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];