Delegate方法不会触发(为null)。 使用ARC
我无法启动我的委托方法。 我有以下代码:
@class Location; @protocol LocationDelegate - (void)location:(Location*)location foundLocation:(CLLocation*)location; - (void)location:(Location*)location failedToLocateUserWithError:(NSError*)error; @end @interface Location : NSObject { __unsafe_unretained id delegate; } @property (nonatomic, assign) id delegate; @end ... @implementation Location @synthesize delegate; - (void)startUpdatingLocation { NSLog(@"%@", delegate); // prints '(null)' ... } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"%@", delegate); // prints '(null)' [delegate location:self foundLocation:newLocation]; } @end
用于其他项目的正常工作,所以想知道它是否与ARC有关? 分配Location
的对象确实符合LocationDelegate
。 我也将代表设置为自我。 但委托方法只是没有触发,如果我输出它,它就是null。
谢谢
从您在此处粘贴的代码开始,我看不到任何与您的代理相关的内容(保留的引用)。
您粘贴的所有内容都会显示您不会让代码保留此代码。
如果这就是你想要的那样,那么你最好确保它被保留在其他地方 – 否则ARC会正确地得出结论,因为没有人对代表有强烈的(保留的)引用,所以释放它是安全的(并且是正确的)。
分配Location的对象确实符合LocationDelegate。 我也将代表设置为自我。
谁强烈提到这个对象?
无论您在何处分配Location
,您是否已分配了代表? 例如: Location *location = [[Location alloc] init]; location.delegate = self;
Location *location = [[Location alloc] init]; location.delegate = self;