我在Popover中创建了倒计时。倒计时持续1分钟。问题是当我显示弹出窗口时,倒计时总是重置为1分钟并重复倒计时。
我在viewdidload方法中执行[self setimer]。
-(void)countdown{
seconds = seconds - 1;
static int minutes;
minutes = seconds / 60;
static int secondes;
secondes = seconds - (minutes * 60);
NSString *timeroutput = [NSString stringWithFormat:@"%2.d:%2d", minutes, secondes];
countdown_label.text = timeroutput;
if (seconds == 0) {
[countdown_timer invalidate];
countdown_timer = nil;
}
}
-(void) setTimer{
seconds = 1500;
countdown_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countdown) userInfo:nil repeats:YES];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSString *identifier = segue.identifier;
if ([identifier isEqualToString:@"popover_time"]) {
UIViewController *dvc = segue.destinationViewController;
UIPopoverPresentationController *ppc = dvc.popoverPresentationController;
if (ppc) {
if ([sender isKindOfClass:[UIButton class]]) { // Assumes the popover is being triggered by a UIButton
ppc.sourceView = (UIButton *)sender;
ppc.sourceRect = [(UIButton *)sender bounds];
}
ppc.delegate = self;
}
}
}