【问题标题】:Objective-c iOS - Can't receive delegate methods in my custom classObjective-c iOS - 无法在我的自定义类中接收委托方法
【发布时间】:2012-09-22 05:43:45
【问题描述】:

我的自定义类需要接收“didUpdateToLocation”CLLocationManagerDelegate 方法,但是我似乎无法使以下代码工作。

头文件。

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface MyCurrentLocation : NSObject<MKAnnotation,CLLocationManagerDelegate,MKMapViewDelegate>
{
    CLLocationCoordinate2D coordinate;
}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) CLLocationManager *locationManager;

-(MyCurrentLocation *)init;

@end

实现文件。

#import "MyCurrentLocation.h"

@implementation MyCurrentLocation

-(BLCurrentLocation *)init
{
    self = [super init];
    if (self) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.distanceFilter = kCLDistanceFilterNone;
        _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        [_locationManager startUpdatingLocation];
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"Did we receive a location?");
}

【问题讨论】:

    标签: objective-c delegates cllocationmanager


    【解决方案1】:

    您必须响应其他委托方法,例如错误回调。此外,检查 CLLocationManager 的 + (CLAuthorizationStatus)authorizationStatus 类方法以确定您是否能够使用位置服务。

    【讨论】:

    • 它工作:)。在CLLocationManager 的文档中,我认为“响应位置事件”部分中定义的两种方法是可选的。再次感谢!
    【解决方案2】:

    我不知道为什么,但也许你可以尝试实现这个委托方法,看看是否无法注册位置服务。

    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please make sure Location Service is ON" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
        [alertView show]; 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-13
      • 1970-01-01
      相关资源
      最近更新 更多