创建一个计时器以超时并弹出警报
我正在尝试使用 CLLocationManger 获取最近的位置。如果我无法在 30 秒内收到邮件,我希望弹出一个警报,提示“我们现在遇到问题”或类似的内容。在我的 CLLocationManager 委托中,我执行以下操作:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
NSTimer *timer;
if (abs(howRecent) < 1.0) { // process time if time is less than 1.0 seconds old.
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCount:) userInfo:nil repeats:NO];
return;
}
我的 updateCount 方法:
- (void)updateCount:(NSTimer *)aTimer {
count++;
NSLog(@"%i", count);
if (count == 30) {
[aTimer invalidate];
NSLog(@"Timer invalidated");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server Down" message:@"Sorry, but we cannot find your location at this time. Please try again later." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
但我没有看到我的警报被触发。我尝试使用重复计时器,但在达到 30 秒并显示警报后,它会再次重复。所以我不太确定我在这里做错了什么。谢谢!
I'm trying to get a recent location using CLLocationManger. If I cannot get one within 30 seconds, I want an alert to pop up saying that "we're having problems right now" or something along those lines. In my CLLocationManager Delegate, I do this:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
NSTimer *timer;
if (abs(howRecent) < 1.0) { // process time if time is less than 1.0 seconds old.
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCount:) userInfo:nil repeats:NO];
return;
}
My updateCount method:
- (void)updateCount:(NSTimer *)aTimer {
count++;
NSLog(@"%i", count);
if (count == 30) {
[aTimer invalidate];
NSLog(@"Timer invalidated");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server Down" message:@"Sorry, but we cannot find your location at this time. Please try again later." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
But I don't see my alert get fired. I tried with a repeating timer, but after it hits 30 seconds, and the alert is shown, it repeats again. So I'm not quite sure what I'm doing wrong here. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是对 updateCount 方法的一个小更新:
不要在 locationManager 方法中使用
NSTimer *timer;
。相反,直接调用timer = [[NSTimer....]]方法,我认为因为updateCount方法中有一个参数,即NSTimer,并且我们使用不同的NSTimer实例,所以这里基本上是问题。尝试在 updateCount 方法中不使用参数并使 .h 文件中声明的计时器无效。just a small update to updateCount method :
Don't use
NSTimer *timer;
in locationManager method. instead directly call timer = [[NSTimer....]] method and I think because there is one argument in updateCount method i.e. NSTimer and we are using different NSTimer instance so here is basically problem. Try without using argument in updateCount Method and invalidate the timer that is declared in .h file.我检查了文档并发现了以下段落。
因此,要使计时器无效,请尝试调用 PerformSelector。
我不确定它是否会有所帮助,但如果它有效,请在此处发布,以便其他人也可以利用。
I checked the documentation and found following paragraph in that.
So, for invalidating timer try calling performSelector.
I am not sure weather it will help or not but if it works then please post here so that other too can take advantage.