为什么我的委托方法只在 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
}
干杯!