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

时间:2013-04-01 19:36:15

标签: iphone location cllocation

我想知道是否有人可以再次帮助我?我已将我的位置编码从我的View控制器移动到NSObject中。然后我从App Delegate

调用了这个
- (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:
}

我已经设置了一个计时器,以便我希望这个过程再次运行

-(void)recheckLocation
{
  //Updating Location
  [Location sharedLocation];
  NSLog(@"Timer Triggered");
}

唯一的事情是当计时器触发共享位置时不会再次更新?请任何人可以提前帮忙吗?非常感谢,Jon。

#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];

}

1 个答案:

答案 0 :(得分:0)

这里你定义的是一个单一的位置,共享位置的目的似乎没有更新位置,如代码注释中所述

sharedInstance所做的是返回一个初始化的引用,并在整个代码中的任何地方使用相同的内容。它为您提供了返回的位置实例,您不会在任何地方检索并使用它。您只需调用它但是不要用它。

定义一种更新位置的方法,并在使用

从该位置获取内存后调用它
Location *sharedLoc=[Location sharedLocation];

并调用方法将位置更新为

[sharedLoc updateLocation];

Location.h

-(void)updateLocation;

位置.m

-(void)updateLocation
{
//Code for updation purpose
}