问题
I have a view with a couple of buttons whick I use to present UIImagePickerControllers (both camera and media pickers). When I create the UIImagePickerControllers, I give them diffrent names and present them modaly as you can see in the code below:
- (void) startMediaBrowserFromViewControllerForBGImage {
UIImagePickerController * mediaUIForBGImage = [[UIImagePickerController alloc]init];
//i also set a bunch of different settings here, but it doesn't matter for this purpose
mediaUIForBGImage.delegate = self;
[self presentModalViewController: mediaUIForBGImage animated:YES];
}
Now as you can also see in the code, I set the delegate to self
, and that is what I do with all the UIImagePickerControllers, but the problem is - in the delegate methods, how do I know which UIImagePickerController called the delegate method. Because the actions I need to take with the media is different for each UIImagePickerControllers. In the delegate method I do get the UIImagePickerController
parameter, but it always have the name that it is assigned from the methods (picker). What can I use to know which UIImagePickerController to refer to?
回答1:
I think you can store a reference to each picker and then check if it is the same instance. Though I would question using multiple different picker instances, can you just configure a single one? Also they return pretty much the same results in any configuration, you can investigate result dictionary for specific needs.
self.oneImagePicker = [UIImagePickerController ...]
self.twoImagePicker = [UIImagePickerController ...]
//In delegate
if ([self.oneImagePicker isEqual:picker]) ...
回答2:
One solution for you might be this:
If you set the "tag
" value of your various pickers, one of the parameters sent to the delegate methods will include the picker that caused the delegate method to fire, and if you look at the "tag
" value of it, you'll know which picker it was.
Another solution might be to set each picker to an ivar or a property and then compare the picker parameter sent to the delegate method to see which one equals which ivar or property (i.e. are the object addresses the same?).
回答3:
You can either workout which picker you are handling in the delegate - examine the tag property for example - or create a UIPopoverController subclass to drive your popover and make it the delegate of the ImagePicker. It will need to pass the selected information back to the main class - You can do this by implementing the popoverControllerShouldDismissPopover delegate method and examining a property of your subclass
来源:https://stackoverflow.com/questions/12328859/identifying-different-pickers-in-uiimagepickercontroller-delegate