如何以每1秒的延迟运行循环?

时间:2018-03-16 10:54:46

标签: ios objective-c apple-watch

我正在为Apple的Watch应用程序实现警报功能。所以在这里我必须将当前时间与用户时间进行比较,并且我必须每1秒获得当前时间,以便我已经实现了如下所示的代码,但这不起作用。任何人都可以在Objective-C中发布适当的代码。

for (int i=0; i<86400; i++)
{  
    if ([getTotalDate isEqualToString:self.curTime])
        {
            NSLog(@"It's Alarm Time");
            [self presentControllerWithName:@"FifthViewController" context:nil];
            break;
        }
    else
        {
            NSLog(@"not alarm time");
            //[self currentTime];
        }
}

2 个答案:

答案 0 :(得分:0)

最好的方法是为该特定时间设置本地通知。您应该在FifthViewController中打开didReceiveLocalNotification。您可以查看以下答案,了解如何使用本地通知。

How to implement LocalNotification using objective C?

答案 1 :(得分:-1)

尝试使用Stride

这是Apple提供的解决方案(循环迭代超时限制):Swift control flow with Stride

有些用户可能希望在其UI中使用更少的刻度线。他们可能更喜欢每5分钟一个标记。使用步幅(from:to:by :)功能跳过不需要的标记。

let minuteInterval = 5
for tickMark in stride(from: 0, to: minutes, by: minuteInterval) {
    // render the tick mark every 5 minutes (0, 5, 10, 15 ... 45, 50, 55)
}



let hours = 12
let hourInterval = 3
for tickMark in stride(from: 3, through: hours, by: hourInterval) {
    // render the tick mark every 3 hours (3, 6, 9, 12)
}