0

为什么我的委托方法只在 MainViewController 中调用,而不在 MapViewController 中调用?

定位服务.h

@protocol LocationServiceDelegate <NSObject>
@optional
- (void) currentLocation: (CLLocation*) location;
@end

@property (nonatomic, assign) id delegate;

定位服务.m

if ([delegate respondsToSelector:@selector(currentLocation:)])
{
    [delegate currentLocation:newLocation];
}

这里方法被调用(在 MainViewController 中)

主视图控制器.h

#import "LocationService.h"
@interface MainViewController : UIViewController <LocationServiceDelegate>

主视图控制器.m

locationService = [[LocationService alloc] init];
locationService.delegate = self;

- (void) currentLocation: (CLLocation*) location
{
    // some code, this method get called perfectly
}

在我的另一堂课上,同样的程序行不通

MapViewController.h

#import "LocationService.h"
@interface MapViewController : UIViewController <MKMapViewDelegate, LocationServiceDelegate>

MapViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    locationService =[[LocationService alloc] init];

    locationService.delegate = self;
}


- (void)currentLocation:(CLLocation *)location
{
  // this method does not get called
}

干杯!

4

1 回答 1

0

您是否在某处将委托设置为零,因为代码对我来说看起来不错?请使用中断指针查看它是否进入 MapViewController 中的 currentLocation 方法实现。

于 2013-06-28T21:31:35.993 回答