Ios 自定义委托方法只能在一个类中调用

Ios 自定义委托方法只能在一个类中调用,ios,objective-c,delegates,Ios,Objective C,Delegates,为什么我的委托方法只在MainViewController中调用,而不在MapViewController中调用 地点服务 @protocol LocationServiceDelegate <NSObject> @optional - (void) currentLocation: (CLLocation*) location; @end @property (nonatomic, assign) id delegate; 此处调用方法(在MainViewController中

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

地点服务

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

@property (nonatomic, assign) id delegate;
此处调用方法(在MainViewController中)

MainViewController.h

#import "LocationService.h"
@interface MainViewController : UIViewController <LocationServiceDelegate>
#import "LocationService.h"
@interface MapViewController : UIViewController <MKMapViewDelegate, LocationServiceDelegate>
在我的另一节课上,同样的程序不起作用

MapViewController.h

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

干杯

您是否在某个地方将delegate设置为nil,因为代码在我看来很好?请使用中断指针查看它是否进入MapViewController中的currentLocation方法实现。

在此处添加断点
if([delegate respondsToSelector:@selector(currentLocation:)])
并单步执行。MapViewController的视图实际已加载吗?我的意思是,第二个
viewdiload
方法有没有被调用过?@ludesign:如果我这样做,我只会在MainViewController中的
-(void)currentLocation:(CLLocation*)location
。m@AaronGolden是
viewDidLoad
get called这很奇怪。你的代码中还有一些东西在耍你。将断点放在LocationService方法(init和其他)上,看看发生了什么;或者给我们更多的代码,这样我们可以帮助你更多。谢谢你的回答,但是,正如我上面提到的,我已经使用了断点。很难看到在这样有限的信息下发生了什么,因为您发布的代码看起来很好。您可以发布您如何定义LocationService的委托吗?我已经在LocationService中编辑了我的代码。您可以看到我如何使用给定的属性将委托定义为属性信息,我看不出代码有任何问题。
- (void)viewDidLoad
{
    [super viewDidLoad];
    locationService =[[LocationService alloc] init];

    locationService.delegate = self;
}


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