2012-02-11 70 views
0

我想写一个方法来返回当前位置。当我等待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; 
    } 
} 

回答

1

伺候任何计时器或任何加速度计或用户事件是简单地用一个return语句退出当前的方法回到UI运行循环的方式。

任何你想在等待完成后可以进入定时器或事件回调方法。

+0

这似乎是有道理的。通过与没有GPS相同的方式,我被困在我的应用程序中将gps功能锁定到我的应用程序中。我会在接下来的一两天内处理这个问题并报告它是如何发生的。 – 2012-02-12 06:12:20

+0

立即开始运行。你的建议解决了。我还在Apple的LocateMe示例代码中找到了很好的参考。 – 2012-02-15 02:58:36

相关问题