停止CLLocationManager Objective-C

时间:2011-11-18 16:58:54

标签: objective-c gps cllocationmanager

我只需要更新一次位置,但是当我尝试停止更新时它不会工作......

.h文件:

@protocol CoreLocationControllerDelegate 
@required
- (void)locationUpdate:(CLLocation *)location; // Our location updates are sent here
- (void)locationError:(NSError *)error; // Any errors are sent here
@end

@interface CoreLocationController : NSObject <CLLocationManagerDelegate>
{
    CLLocationManager * locMgr;
    id delegate;
}
@property (nonatomic, retain) CLLocationManager *locMgr;
@property (nonatomic, assign) id delegate;

@end

.m文件:

@synthesize locMgr,delegate;

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

        if(self != nil) {
            self.locMgr = [[[CLLocationManager alloc] init] autorelease]; // Create new instance of locMgr
            self.locMgr.delegate = self; // Set the delegate as self.
        }

        return self;
    }

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
        if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) 
        {  // Check if the class assigning itself as the delegate conforms to our protocol.  If not, the message will go nowhere.  Not good.
            [self.delegate locationUpdate:newLocation];
            [self.locMgr stopUpdatingHeading];
            [self.locMgr setDelegate:nil];
        }
    }

    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
        if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) {  // Check if the class assigning itself as the delegate conforms to our protocol.  If not, the message will go nowhere.  Not good.
            [self.delegate locationError:error];
        }
    }

我在课堂上尝试过,当我在我的viewcontroller上声明它时,但两者似乎都无法工作......

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用-stopUpdatingLocation而不是标题?