我正在开发一个 iPAD 应用程序,我想每隔 10 秒将表单的内容自动保存到 SQLITE 中。现在,如果我按下保存按钮,它就会保存到数据库中。有什么方法可以每隔 10-15 秒自动保存我在表格中写的任何内容。帮我解决这个问题。
最佳答案
使用 NSTimer 并每 x 分钟执行一次保存。代码看起来像这样。它是代码的修改版本 here .
@interface MyController : UIViewController
{
@private
NSTimer * countdownTimer;
NSUInteger remainingTicks;
}
-(IBAction)doCountdown: (id)sender;
-(void)handleTimerTick;
-(void) saveData;
@end
@implementation MyController
// { your own lifecycle code here.... }
-(IBAction)doCountdown: (id)sender
{
if (countdownTimer)
return;
remainingTicks = 60;
[self saveData];
countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @selector(handleTimerTick) userInfo: nil repeats: YES];
}
-(void)handleTimerTick
{
remainingTicks--;
[self updateLabel];
if (remainingTicks <= 0) {
[countdownTimer invalidate];
countdownTimer = nil;
}
}
-(void) saveData
{
//Save your data here
}
@end
关于ios - 自动将屏幕内容保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21757456/