Search code examples
iosobjective-cuiimagepickercontrollerpicker

How to distuingish between UIImagePickerController in didFinishPickingMediaWithInfo


I have two UIImageViews in which I want to load two different images. I have two buttons which triggers a UIImagePickerController and calls my

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

method.

Is there a way to distinguish from which picker the method above is called?


Solution

  • You can make an instance of UIButton

    @interface YourClass ()
    {
       UIButton *_selectedButton;
    }
    

    and before triggering UIImagePickerController, you can save the button in the instance variable

    -(void)button1Clicked:(UIButton *)button1{
          _selectedButton = button1;
         // call UIImagePickerController
    } 
    
    -(void)button2Clicked:(UIButton *)button2{
          _selectedButton = button2;
          // call UIImagePickerController
    } 
    
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{   
          if (_selectedButton == button1){
             // perform your logic 
          } else if (_selectedButton == button2){
            // perform your logic 
          }
    }