jape
jape

Reputation: 2901

Perform different function based on button that activates UIImagePickerController

I have two buttons, and when each is pressed, a UIImagePickerController will appear.

I can select and retrieve the photo just fine. However, I want to do something different based on which button started the action.

Is there a way to determine which button caused the initial UIImagePickerController to appear in the didFinishPickingImage call?

Upvotes: 0

Views: 36

Answers (2)

user444333222
user444333222

Reputation: 161

You can assign the tag property for both buttons and then when you create the UIImagePickerController instance you can assign the button tag property for the button that was touched to the picker.view.tag property of the UIImagePickerController instance.

Then in your delegate call back for UIImagePickerController you can check the picker's view.tag property to determine which button was touched when creating the uiimagepickercontroller

mybutton.tag = 100;
[myButton addTarget:self action:@selector(displayTheUIImagePickerController:)     forControlEvents:UIControlEventTouchUpInside];



-(void)displayTheUIImagePickerController:(id)sender
{
    NSInteger buttonTag = [sender tag];

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.view.tag = buttonTag;

//Finish setting up picker controller and then present
}



- (void)imagePickerController: (UIImagePickerController *)picker
    didFinishPickingImage: (UIImage *)i
              editingInfo: (NSDictionary *)editingInfo {

    NSInteger buttonTag = picker.view.tag;


    switch (buttonTag) {
        case 100:
            //Do something for button with tag 100
            break;

        case 101:
            //Do something for button with tag 101
            break;

        default:
            break;
    }

}

Upvotes: 0

Léo Natan
Léo Natan

Reputation: 57040

There is no magic. You need to somehow save your state, and when the delegate method is called, determine your state and act accordingly. The easiest here, is to add a property or instance variable to your controller and update it with a unique value (such as enum) for each button.

Upvotes: 2

Related Questions