我想将我的变量(浮点数)自动保存在我已经创建的属性列表中。我可以使用一个按钮执行此操作,效果很好,但我想使用IOS 7自动执行此操作(某些方法在IOS 6或7中已弃用)。
replyToApplicationShouldTerminate
不起作用...
我用这样的按钮保存在属性列表中:
-(IBAction)apply:(id)sender
{
{
[nameC1 resignFirstResponder];
[nameC2 resignFirstResponder];
[nameC3 resignFirstResponder];
[nameC4 resignFirstResponder];
[nameC5 resignFirstResponder];
nom1 = [nameC1 text];
nom2 = [nameC2 text];
nom3 = [nameC3 text];
nom4 = [nameC4 text];
nom5 = [nameC5 text];
v1 = [NSString stringWithFormat:@"%d", compteur1];
v2 = [NSString stringWithFormat:@"%d", compteur2];
v3 = [NSString stringWithFormat:@"%d", compteur3];
v4 = [NSString stringWithFormat:@"%d", compteur4];
v5 = [NSString stringWithFormat:@"%d", compteur5];
//Write in Data.plist
NSString *error;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: nom1, nom2, nom3, nom4, nom5, v1, v2, v3, v4, v5, nil] forKeys:[NSArray arrayWithObjects: @"nomCompteur1", @"nomCompteur2", @"nomCompteur3", @"nomCompteur4", @"nomCompteur5", @"valeurCompteur1", @"valeurCompteur2", @"valeurCompteur3", @"valeurCompteur4", @"valeurCompteur5",nil]];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
if(plistData) {
[plistData writeToFile:plistPath atomically:YES];
}
else {
NSLog(@"error");
//[error release];
}
}
}
在装入笔尖之后,在每个文本字段中注册有关值更改的通知:
- (void)awakeFromNib
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(UITextFieldTextDidChange:)
name:UITextFieldTextDidChangeNotification
object:nameC1];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(UITextFieldTextDidChange:)
name:UITextFieldTextDidChangeNotification
object:nameC2];
...
}
当文本更改时,保存数据:
- (void)UITextFieldTextDidChange:(NSNotification*)notification
{
[self save];
}
- (void)save
{
...
}
您可能想花一些点时间,然后dispatch_after()
等待〜0.5秒再执行保存。这样,用户将不会注意到保存是否有点慢。但是我怀疑在这种情况下保存会非常快。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句