Delegate method won't fire (is null). Using ARC

放肆的年华 提交于 2019-12-23 04:27:04

问题


I'm having trouble getting my delegate method to fire. I have the following code:

@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 <LocationDelegate> delegate;
}

@property (nonatomic, assign) id <LocationDelegate> 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

Used to work fine for other projects, so wondering if it has to do with ARC? The object that allocates Location does conform to LocationDelegate. I am also setting the delegate to self. But the delegate method just isn't firing, and if I output it, it's null.

Thanks


回答1:


From just the code you've pasted here, I don't see anything that is holding on to your delegate (a retained reference).

Everything you've pasted specifically shows you going out of your way to not have the delegate retained by this code.

If thats how you want it to be, then you better be sure it's been retained elsewhere - otherwise ARC will correctly conclude that since no one has a strong (retained) reference to the delegate, that it's safe (and proper) to release it.

The object that allocates Location does conform to LocationDelegate. I am also setting the delegate to self.

And who has a strong reference to that object?




回答2:


Wherever you are allocating Location have you assigned the delegate? Eg: Location *location = [[Location alloc] init]; location.delegate = self;



来源:https://stackoverflow.com/questions/7852308/delegate-method-wont-fire-is-null-using-arc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!