iPhone - 如何处理多个UIImagePickerControllerDelegate?

时间:2013-05-07 04:45:05

标签: iphone delegates uiimagepickercontroller

我正在开发一个简单的应用程序扫描QR码。要扫描QR码,我使用一种委托方法,如下所示。

- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info

此外,我还有另一种源类型相机的委托方法来拍照。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo 

我有2个按钮用于扫描QR和拍照。但问题是当我拍摄新照片时

- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
调用

方法。怎么解决这个问题。当我选择照片按钮时,我需要调用特定的委托方法,当我打电话选择QR按钮时,我需要调用另一个委托方法。

1 个答案:

答案 0 :(得分:0)

首先 - 值得一提的是这个方法:

- (void)imagePickerController:(UIImagePickerController *)picker 
        didFinishPickingImage:(UIImage *)img 
                  editingInfo:(NSDictionary *)editInfo 
自iOS 3.0起,

已弃用。现在使用的唯一方法是

- (void)imagePickerController:(UIImagePickerController *)picker  
didFinishPickingMediaWithInfo:(NSDictionary *)info

(从字典中获取图像参考)

要回答这个问题:您需要一种方法来识别委托回调中的每个picker。一种方法是创建几个属性,每个选择器一个属性,并在调用选择器时设置属性...

@property (nonatomic, weak) UIImagePickerController* QRpicker;
@property (nonatomic, weak) UIImagePickerController* cameraPicker;

创建您的QRPicker ......

 UIImagePickerController* QRpicker = [[UIImagePickerController alloc] init];
         //configure QRPicker
 [self presentViewController:QRpicker];
 self.QRpicker = QRpicker;

对cameraPicker执行相同的操作

然后你只需要检查哪种类型的选择器调用了委托方法

- (void)imagePickerController:(UIImagePickerController *)picker  
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  if ([picker isEqual:self.QRPicker])
      //code for QRPicker delegate
  else 
      //code for cameraPicker delegate