简体   繁体   English

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

[英]Not calling delegate on timer Objective-C

I all I wonder if anybody can help me once again? 我很想知道是否有人可以再次帮助我? I have moved my location coding from my View controller into an NSObject. 我已经将位置编码从View控制器移到了NSObject中。 I have then called this from the App Delegate 然后我从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:
}

I have set a timer for when I want this process ran again 我已经设置了一个计时器,该计时器何时可以再次运行

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

The only thing is when the timer triggers the sharedlocation does not get updated again?? 唯一的事情是当计时器触发共享位置时不会再次更新? Please can anybody help in advance? 请有人可以提前帮助吗? Many thanks, 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];

}

Well what yo defined here is a singleton of Location and the purpose of shared location does not seem to update the location as it says in the code comments 好吧,这里定义的是位置的单例,共享位置的目的似乎没有更新代码注释中所指出的位置

what the sharedInstance does is return a reference that is once initialized and is used the same everywhere in the whole code.It gives you the location instance in return, which you are not retrieving anywhere and using it.You just call it but not use it. sharedInstance所做的是返回一个引用,该引用一旦被初始化并在整个代码中的各处使用相同,它将为您提供位置实例作为回报,您无需在任何地方检索并使用它,您只需调用它就可以使用它。

Define a method to update location and call it after getting the memory from the location using 定义一种更新位置的方法,并在使用以下方法从位置获取内存后调用该方法

Location *sharedLoc=[Location sharedLocation];

and call the method to update the location as 并调用方法将位置更新为

[sharedLoc updateLocation];

Location.h 位置.h

-(void)updateLocation;

in Location .m 在位置.m

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM