首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >未在计时器Objective-C上调用委托

未在计时器Objective-C上调用委托
EN

Stack Overflow用户
提问于 2013-04-02 03:36:15
回答 1查看 142关注 0票数 0

我想知道是否有人能再帮我一次忙?我已经将我的位置编码从我的视图控制器移到了NSObject中。然后,我从App Delegate调用了它

代码语言:javascript
复制
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Updating Location
[Location sharedLocation];

//Timer for reloading the XML
recheckTimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(recheckLocation) userInfo:nil repeats:YES];
return YES:
}

我已经设置了一个计时器,当我想让这个进程再次运行时

代码语言:javascript
复制
-(void)recheckLocation
{
  //Updating Location
  [Location sharedLocation];
  NSLog(@"Timer Triggered");
}

唯一的问题是,当计时器触发时,共享位置不会再次更新??有没有人能提前帮我?非常感谢,乔恩。

代码语言:javascript
复制
#import "Location.h"

@implementation Location


@synthesize locationManager;


- (id)init {
  self = [super init];

  if(self) {
    self.locationManager = [CLLocationManager new];
    [self.locationManager setDelegate:self];
    [self.locationManager setDistanceFilter:500];//Metres
    [self.locationManager setHeadingFilter:90];
    [self.locationManager startMonitoringSignificantLocationChanges];
  }
  return self;
}


+ (Location*)sharedLocation {
  static Location* sharedLocation;
  if(!sharedLocation) {
    @synchronized(sharedLocation) {
      sharedLocation = [Location new];
    }
  }

  return sharedLocation;
}


//LOCATION CODING
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError' %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Failed to Get Your Current Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

 [errorAlert show];
}


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
  NSLog(@"didUpdateToLocation: %@", newLocation);
  CLLocation *currentLocation = newLocation;

  if (currentLocation != nil) {

    //Resolve Web Address
    webAddressResolved = [NSString stringWithFormat:@"XZYYFASDFA%f,%f.xml", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude];
    NSLog(@"Address Resolved %@", webAddressResolved);
  }

  //Stop Location Manager
  [locationManager stopMonitoringSignificantLocationChanges];

}
EN

回答 1

Stack Overflow用户

发布于 2013-04-02 14:43:34

您在这里定义的是位置的单例,而共享位置的目的似乎并不像代码注释中所说的那样更新位置

sharedInstance所做的是返回一个引用,该引用在初始化后在整个code.It中的任何地方都被使用。返回给您的是location实例,您没有在任何地方检索它,并且使用it.You只调用它而不使用它。

定义一个更新位置的方法,并在从该位置获取内存后调用该方法

代码语言:javascript
复制
Location *sharedLoc=[Location sharedLocation];

并调用该方法将位置更新为

代码语言:javascript
复制
[sharedLoc updateLocation];

Location.h

代码语言:javascript
复制
-(void)updateLocation;

在位置.m

代码语言:javascript
复制
-(void)updateLocation
{
//Code for updation purpose
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15750431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档