[英]Not calling delegate on timer Objective-C
我很想知道是否有人可以再次幫助我? 我已經將位置編碼從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");
}
唯一的事情是當計時器觸發共享位置時不會再次更新? 請有人可以提前幫助嗎? 非常感謝,喬恩。
#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];
}
好吧,這里定義的是位置的單例,共享位置的目的似乎沒有更新代碼注釋中所指出的位置
sharedInstance所做的是返回一個引用,該引用一旦被初始化並在整個代碼中的各處使用相同,它將為您提供位置實例作為回報,您無需在任何地方檢索並使用它,您只需調用它就可以使用它。
定義一種更新位置的方法,並在使用以下方法從位置獲取內存后調用該方法
Location *sharedLoc=[Location sharedLocation];
並調用方法將位置更新為
[sharedLoc updateLocation];
位置.h
-(void)updateLocation;
在位置.m
-(void)updateLocation
{
//Code for updation purpose
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.