我正在尝试编写一个返回当前位置的方法。当我等待30秒,1分钟或2分钟(在设置中设置)时,我显示警报视图。我允许用户绕过定时器等待,其中alertView按钮接受我将在警报视图中显示和更新的当前准确度。我的代码中有几个漏洞我需要帮助。本质上,它是知道如何让getCurrentLocation方法等待计时器。我不能只是使用延迟,因为我计划在用户点击按钮或满足位置精度时强制计时器到期(这在计时器中检查)。这是代码:
- (void)timerFireMethod {
static int elapsedTime = 0;
elapsedTime++;
if (elapsedTime >= gpsTimeout) {
[locationManager stopUpdatingLocation];
elapsedTime = 0; // reset static variable
// !!! How do I do the following !!!
// do something to allow getCurrentLocation to return
}
if ((currentLocation.horizontalAccuracy <= gpsDesiredAccuracy) & (currentLocation.verticalAccuracy <= 2*gpsDesiredAccuracy)) {
[locationManager stopUpdatingLocation];
elapsedTime = 0; // reset static variable
// do something to allow getCurrentLocation to return
}
}
- (CLLocation *)getCurrentLocation {
// check if current accuracy is good enough and return if true
if ((currentLocation.horizontalAccuracy <= gpsDesiredAccuracy) & (currentLocation.verticalAccuracy <= 2*gpsDesiredAccuracy)) {
[locationManager stopUpdatingLocation];
return currentLocation;
} else {
// show alert with count down timer
UIAlertView *gpsAlertView = [[UIAlertView alloc] initWithTitle:nil message:@"tbd put in timer and list location accuracy updates" delegate:self cancelButtonTitle:@"Continue With Current Accuracy" otherButtonTitles:nil];
[gpsAlertView show];
// start timer
NSTimer *gpsTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self selector:@selector(timerFireMethod:)
userInfo:nil repeats:YES];
// !!! How do I do the following !!!
// wait for when timer expires,
// the user to press "Continue With Current Accuracy" button (can force timer to expire?)
return currentLocation;
}
}
答案 0 :(得分:1)
等待任何计时器或任何加速度计或用户事件的方法是使用return语句将当前方法退回到UI运行循环。
等待之后你想做的任何事情都可以进入计时器或事件回调方法。