为什么我的CLLocationManagerDelegate方法没有被调用?

时间:2014-07-29 13:57:56

标签: ios objective-c

致电课程......

     @implementation MenuViewController

     - (void)viewDidLoad {
         [super viewDidLoad];

         FuelSiteLocator *siteLocator = [[FuelSiteLocator alloc]init];
         [siteLocator getStations:@"Unleaded Regular"];

         ...
     }

这是不起作用的课程......

    @implementation FuelSiteLocator {
        CLLocationManager *locationManager;
    }

    -(id) initWithSearchType {
        locationManager = [CLLocationManager new];
        [locationManager setDelegate:self];
        return self;
    }

    -(void) getStations:(NSString *)search {
        [self setSearchType:search];
        [locationManager startMonitoringSignificantLocationChanges];
    }

    -(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
        NSLog(@"%@", error.description);
    }

    -(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
        [self getGasStations:[locations lastObject]];
    }

    -(void) getGasStations:(CLLocation *) currentLocation {
        NSLog(@"Hello Gas Stations..");
    }

没有调用委托方法,我不知道为什么......在FuelSiteLocator.h中我正在使用CLLocationManagerDelegate协议

****问题已解决******* ARC在调用getStations后释放siteLoactor对象。委托方法永远不会有机会

1 个答案:

答案 0 :(得分:0)

如前面评论中所述,您的init方法不正确。

尝试:

-(id) initWithSearchType:(NSString *)search {
    self = [super init];
    if (self) {
        self.searchType = search;
        self.locationManager = [CLLocationManager new];
        self.locationManager.delegate = self;
    }
    return self;
}

(这假设该类继承自NSObject,因为我还没有看到类定义。)