Reputation: 1959
I recently refactored my app to implement the Command pattern. To that end, I am trying to silo all of my functionality into the appropriate command classes. I have successfully implemented the pattern for all of my commands, with one exception. I have a command called HarvestPhotoCommand
that launches a UIImagePickerController
, and I want to use that command class as the picker's delegate. Seems straightforward enough, but for some reason the delegate methods are not being invoked.
Here's my code:
//Header file
@interface WSHarvestPhotoCommand : WSCommand
<
UINavigationControllerDelegate,
UIImagePickerControllerDelegate,
MWPhotoBrowserDelegate
>
@end
//From the super class header file:
@property (nonatomic, weak) WSMapViewEngine *mapViewEngine;
//Implementation file
-(void)execute
{
_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePicker.delegate = self;
_imagePicker.navigationBar.barStyle = [WSAppSettings sharedSettings].theme.barStyle;
[self.mapViewEngine.mapVC presentViewController:_imagePicker animated:YES completion:nil];
}
#pragma mark - UIImagePickerControllerDelegate
#pragma mark -
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(@"Please hit me!");
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
NSLog(@"Or me!");
}
WSCommand
is an NSObject
, as is self.mapViewEngine
. self.mapViewEngine.mapVC
is a UIViewController
.
I have tried two experiments thus far. First, I set the delegate protocols on the mapViewEngine
and made that the delegate; second, I did the same for the mapVC
. Both of these experiments worked, meaning the delegate methods were invoked in each of those classes. That proved my UIImagePicker
implementation is correct.
So why will the same not work for my command class? Both it and the mapViewEngine
are NSObject so I know that's not the problem. I suppose I could make the mapViewEngine
or the mapVC
the delegate, but the whole reason I'm doing this is to de-couple my classes. I've been trying to solve this all night, so I will accept ANY thoughts or suggestions at this point.
Upvotes: 1
Views: 78
Reputation: 4728
This (suggested in my previous comment) is a memory management issue, the delegate callbacks are being fired but the object which had been delegate has already been released because it was not backed by a strong pointer.
As DisableR points out the delegation connection did not affect the objects retain count (and you didn't get an exception like you might have two years ago because the delegate has been nil'd out..)
Upvotes: 1
Reputation: 6030
If you are using ARC, delegate
of UIImagePickerController
is supposed to be weak
. Which means that after object which is UIImagePickerControllerDelegate
dies delegate
value of UIImagePickerController
becomes nil
automatically.
Your WSHarvestPhotoCommand
object dies in the memory before you make any taps on UIImagePickerController
.
Upvotes: 2